Webocreation

Tuesday, November 17, 2009

C++ note









Untitled Document







What do you mean by procedural programming?


Conventional programming using high level languages such as COBOL, C, and FORTAN is known as procedure-oriented programming. Procedural programming employs top down programming approach where a program is viewed as a sequence of task to be performed. A number of functions are written to implement these tasks.


Characteristics of procedural programming language


Emphasis is on doing things.


What do you mean by virtual destructor?


                Just like ordinary virtual functions destructors can also be declared as virtual, whereas constructor cannot be Virtual. When a derived object pointed is deleted, destructors of all its base classes as well as destructor of derived classes are involved if we declare base class pointer as virtual.


But if the destructor in base class is non-virtual then only the destructor of the base class is invoked when the base class pointer pointing to the derived class object is deleted.



//Code of virtual destructor


#include<iostream.h>


#include<string.h>


class parent


{


                protected:


                                char*name;


                public:


                                parent (char*x)


                                {


                                                name=new char[strlen(x)+1];


                                                strcpy(name x);


                                }


                                virtual void show()


                                {


                                                cout<<"parents name:"<<name;


                                }


                                virtual ~parent()


                                {


                                                delete name;


                                                cout<<"parent destroyed"<<endl;


                                }


};


class child:public parent


{


                private:


                                char*name;


                public:


                                child(char*x1,char x2):parent(x1)


                                {


                                                name=new char[strlen(x2)+1];


                                                strcpy(name,x2);


                                }


                                void show()


                                {


                                                cout<<endl<<"childs name:"<<name;


                                }


                                ~child()


                                {


                                                delete name;


                                                cout<<endl<<"child destroyed";


                                }


};


void main()


{


                parent *p1;


                p1=new parent("parent");


                p1->show();


                delete p1;


                p1=new child (parent from child class:."child");


                p1->parent::show();


                delete p1;


                getch();


}


Output


Parent name: parent


Parent destroyed


Child name: child


Parent name: parent from child class


Child destroyed


Parent destroyed



If we remove virtual keyword in the definition of destructor of base class, the second last output line (“child destroyed”) is not obtained virtual function used in following situation:


It is used when one class needs to delete object of derived class that are addresses by the base class pointer and invoked a base class destructor to release resources allocated to it.


When delete operation is performed on an object by a pointer or reference the program will first call the object destructor instead of the destructor associated with the pointer or reference  type if the destructor is defined as virtual.



“this” pointer



C++ uses a unique keyword called ‘this’ to represent an object that invokes a member function


This is a pointer that points to the object for which this function was called.


-for e.g. the function call A.max() will set the pointer this to the address of the object A


This unique pointer is automatically passed to a member function when it is called. The pointer ‘this ’ acts as an implicit argument to call the member functions


This pointer refers to an object that currently invokes a member functions


Syntax:


class  test


{


                Int data;


                public:


                                func(){..;}


                                func()


                                {


                                                this->data;


                                                this->func();


                                }


};


Complex add(complex c2)


{


                Complex t;


                t.real=real+c2.real;


                t.imag=imag+c2.imag;


                return *this;


}


Therefore, this pointer can be used to return objects


For e.g.


#include<iostream.h>


Class person


{


                Int age;


                Public:


                                person(int a)


                                {age=a;}


                                Void disp;ay()


                                {Cout<<age;}


                                Person greater(person&);


};


Person person::greater(person &x)


{


                If(age,x.age)


                                return x;


                else


                                return *this;


}


void main()


{


                person per(15), per(13);


                person p=per.greater(pet);


cout <<”the greater age is :”;


p.display();


}


Output


The greater age is:15



 



TEMPLATES


Templates are one of the features added in C++ recently. It is a new concept which enable is to define generic classes and functions and thus provides supports for generic programming and provides flexible


Generic programming is an approach where generic types are used as parameters in algorithms so that they work for variety of suitable data-types and data structure.


A template can be used to create a family of classes or functions. For e.g.: a class template for an array class would enable us to create array of various data-types such ass int array and float array etc.


A template can be considered as a kind of macro when ana object of a specific type is defined for actual use, the template definition for that class is substituted with the required data type. Since a template is defined with a parameter that would be replaced by specific data type at the time of actual use of class or function, the templates are sometime also called vparameterized classes or function.


Class templates


The general format of a class template is:


Template<class T>


Class classname


{


                …………


                //class member specification


                //with anonymous type T


                //when even appropriate


                ……….


};


The syntax for defining an object of a template class is:


classname<type>objectname(arg_list);



#include<>m.h


Const size=3;


template<class t>


class vector


{


t *v;       //type t vector


public:


vector()


{


                v=new T[size];


for(int i=0;i<size;i++)


                v[i]=0;


}


vector(T*a)


{


                for(int i=0;i<size;i++)


                v[i]=a[i];


}


T operator *(vector &y)


{


                T sum=0;


                for(in i=0;i<size;i++)


                                Sum+=this->v[i]*y.v[i];


                return sum;


}


};


Void main()


{


                int x[3]={1,2,3};


                int y[3]={4,5,6};


vector <int> v1;


vector<int> v2;


v1=x;


v2=y;


int R=v1*v2;


cout<<”R=”<<R<<endl;


float x1[3]={1.1, 2.2,3.3};


float x2[3]={4.4,5.5,6.6};


vector<float> v3;


vector<float>v4;


v3=x1;


v4=x2;


float R1=v3*v4;


cout<<”R1=”<<R1;


}



Class template with multiple parameters:



Template <class T1, class T2>


Class classname


{


                -------------------


                --------------------// body of class


}







No comments:

Post a Comment