Constructors Destructors Constructors A constructor is called when

  • Slides: 31
Download presentation
Constructors & Destructors

Constructors & Destructors

Constructors • • A constructor is called when the object comes into existence. Constructor

Constructors • • A constructor is called when the object comes into existence. Constructor is a special member function whose name is same as that of the class in which it is defined and it does not return anything (not even void). Constructor’s primary task is to allocate memory for the object and as a secondary task it may initialize the data members of the object. Constructor is called implicitly, but we can call it explicitly also.

class X { int a; public: X() { }; a=5; } void disp() {

class X { int a; public: X() { }; a=5; } void disp() { cout<<a; } main() { X x 1=X(); x 1. disp(); // Explicit Call X x 2(); x 2. disp(); // Implicit Call } • If the user does not specify the constructor then compiler provides a default constructor into the class. X( ) { // no statement inside the constructor } • It is also called as “Do Nothing Constructor”

Properties of Constructor 1. 2. 3. 4. 5. 6. 7. 8. It should be

Properties of Constructor 1. 2. 3. 4. 5. 6. 7. 8. It should be declared in the public section but we can have private constructors. Constructor is invoked automatically when the object is created. They don’t have any return type, not even void. They can’t be inherited but, the derived class can call the base class constructor. They can have default arguments. Constructors can’t be virtual. We can’t refer to the address of the constructor. An object having a user-defined constructor can’t be made as a member of an union.

Types of Constructor • Default • Parameterized • Copy

Types of Constructor • Default • Parameterized • Copy

Default Constructor • A constructor with no argument. class X { int a; public:

Default Constructor • A constructor with no argument. class X { int a; public: X() { }; a=5; } void disp() { cout<<a; } main() { X x 1=X(); x 1. disp(); // Explicit Call X x 2(); x 2. disp(); // Implicit Call }

Parameterized Constructor Passing parameters to a constructor. • class A { int x, y;

Parameterized Constructor Passing parameters to a constructor. • class A { int x, y; public: A(int a, int b) { x=a; y=b; } main() { A obj 1(5, 2); obj. show(); A obj 2(10, 20); obj 2. show(); A obj 3=(4, 6); obj 3. show(); void show() { }; } A obj 4=4, 6; obj 4. disp(); cout<<“sum is”<<x+y; } // Explicit call // 7 //Implicit Call // 30 //Error

Constructor with one parameter: A special case • In one argument case, we can

Constructor with one parameter: A special case • In one argument case, we can use • either ob(i) ; • or ob=i ; class X { int a; public: X(int k) { }; a=k; } int get a() { return a; } main() { X ob=10; // same as ob(10); cout<<ob. geta(); }

Copy Constructor It is used to copy the content of one object to another

Copy Constructor It is used to copy the content of one object to another object of same class. • Copy constructor takes a reference type as argument otherwise it leads to an infinite loop. • Compiler provides a default copy constructor if the programmer has not specified it explicitly. •

Copy Constructor Example class X { int a; public: X(int k) { }; a=k;

Copy Constructor Example class X { int a; public: X(int k) { }; a=k; } X(X &p) //Copy Constructor { a=p. a; } void disp() { cout<<a; } main() { X x 1(5); X X 2(x 1); //calls the copy constructor X 1. disp(); X 2. disp(); } // 5

Constructor Overloading More than one constructors inside a class which can be differentiated by

Constructor Overloading More than one constructors inside a class which can be differentiated by the compiler at the compile time based on the no of args, type of args or order of args , is called as Constructor Overloading.

Constructor Overloading Example class X { int a, b; public: X() { }; a=b=0;

Constructor Overloading Example class X { int a, b; public: X() { }; a=b=0; } X(int c, int d) { a=b=c; } X(int c, int d) { a=c; b=d; } void disp() { cout<<a<<b; } main () { X x 1(5); x 1. disp(); // 5, 5 X x 2(10, 20); x 2. disp(); // 10, 20 } X x 3; x 3. disp(); //0, 0 x 3=x 2; x 3. disp(); //10, 20

Default arguments in Constructor The parameterized constructor which is having one or more than

Default arguments in Constructor The parameterized constructor which is having one or more than one default arguments is referred as constructor with default argument. class X { int a, b; }; public: X(int c, int d=0) { a=c; b=d; } void disp() { cout<<a<<b; } main() { X x 1(5, 7); X x 2(1); x 1. disp(); x 2. disp(); } // 5, 7 // 1, 0

Ambiguity in Constructor class A { int x; public: }; A() { // Invokes

Ambiguity in Constructor class A { int x; public: }; A() { // Invokes obj 1 cout<<“Hello”; } A(int a=5) { x=a; } void disp() { cout<<x; } main() { A obj 1; obj 1. disp(); A obj 2(10); obj 2. disp(); } Error : A: : A(); A: : A(int); // Ambiguity

Dynamic Constructor The constructors can also be used to allocate while creating objects. This

Dynamic Constructor The constructors can also be used to allocate while creating objects. This will enables the system is allocate right amount of memory for each object whenever the objects are not of the same size , thus resulting in saving of memory & allocation of memory to object at the time of their constructor is known as Dynamic Constructor.

Destructor • • • A destructor is called when the object is destroyed. Destructors

Destructor • • • A destructor is called when the object is destroyed. Destructors are called automatically when the object goes out of scope. The name of the destructor is same as class name and must be preceded by a tilde mark(~). int i=0; class X { int a; public: X() { cout<<“CON”<<++i; } ~x() { cout<<“DES”<<i--; } }; main() { X x 1, x 2; } Output: CON 1 CON 2 DES 1 Destructors are called in the reverse order of constructors.

int i=0; class X { int a; public: X() { cout<<“CON”<<++i; } ~x() {

int i=0; class X { int a; public: X() { cout<<“CON”<<++i; } ~x() { cout<<“DES”<<i--; } }; main() { X x 1, x 2; { X x 3, x 4; } { X x 5; } } Output: CON 1 CON 2 CON 3 CON 4 DES 3 CON 3 DES 2 DES 1

Properties of Destructor • • Name of the destructor is same as the class

Properties of Destructor • • Name of the destructor is same as the class name preceded by ~ mark. It does not have any return type, not even void. It can’t be inherited but the derived class may call the base class destructor. We can have only one destructor in a class i. e. Destructor can’t be overloaded as it does not take any argument. We can’t refer to the address of the destructor. Unlike constructor, it can be virtual. Object having a destructor can’t be made as a member of union.

Friend Function & Friend Class

Friend Function & Friend Class

Why we need Friend Function? • It is a nonmember function which access the

Why we need Friend Function? • It is a nonmember function which access the private members of a class. • The private data of a class can’t be accessed from the outside of the class. • But consider the following situation, There are two classes manager and scientist. We would like to use incom_tax() function to operate on the objects of these classes. • In such situation C++ allows the common function to be made friendly with both the classes.

Friend function declaration class ABC{ }; § § ……… public: ……… friend void xyz();

Friend function declaration class ABC{ }; § § ……… public: ……… friend void xyz(); //declaration The function declaration should be preceded by the keyword friend. The function is defined elsewhere in the program. The function definition does not use either the keyword friend or the scope resolution operator : :

Program to demonstrate friend function class Alpha{ int a; int b; public: void set();

Program to demonstrate friend function class Alpha{ int a; int b; public: void set(); friend void add(Alpha ob 2); //add is declared as friend to class } Alpha void Alpha : : set() //member function set() is defined outside the class { a=10; b=20; } void add(Alpha ob 2) //add() is a normal C++ function { I nt sum= ob 2. a + ob 2. b; cout<<“Sum=“<<sum; } int main(){ Alpha ob 1; ob 1. set(); add(ob 1); return 0; }

Friend Function properties § It is not in the scope of the class to

Friend Function properties § It is not in the scope of the class to which it has been declared as friend. § It can’t be called using the object of that class. Thus has to be invoked like a normal C++ function. § It can be declared either in the public or the private part of a class without affecting its meaning. § Usually it takes objects as arguments.

Using member function of one class as friend of other class Class Alpha{ ….

Using member function of one class as friend of other class Class Alpha{ …. . int fun 1(); // member function of class Alpha …. . }; Class Beta{ …. . friend int Alpha : : fun 1(); // fun 1() of Alpha is friend of Beta …. . }; Here function fun 1() is a member of class Alpha and a friend of class Beta.

class X { int a; public: X(int a 1) { a = a 1;

class X { int a; public: X(int a 1) { a = a 1; } friend void Y : : add(X); }; class Y { int b; public: Y(int b 1) { b = b 1; } void add (X p) { cout<< ( b + p. a) } }; int main() { X x 1(5); Y y 1(10); y 1. add(x 1); return 0; } Member function add() of Y is accessing the private data of X So X must declare add() of Y as its friend

class X { int a; public: X(int a 1) { a = a 1;

class X { int a; public: X(int a 1) { a = a 1; } friend class Y; }; int main() { X x 1(5); Y y 1(10); y 1. add(x 1); y 1. sub(x 1); return 0; } class Y { int b; public: Y(int b 1) { b = b 1; } void add (X p) { cout<< ( b + p. a) } void sub (X p) { cout << ( b - p. a) } }; Member functions add() and sub() of Y is accessing the private data of X So X must declare add() and sub() of Y as its friends otherwise it has to declare the whole Y class as its friend.

Friend Class All the member functions of one class are friend functions of another

Friend Class All the member functions of one class are friend functions of another class Alpha{ …. . int fun 1(); float fun 2(); // member function of class Alpha …. . }; class Beta{ …. . friend int Alpha : : fun 1(); // fun 1() of Alpha is friend of Beta friend int Alpha : : fun 2(); // fun 2() of Alpha is friend of Beta …. . }; class Beta{ …. . friend class Alpha; // All member functions of Alpha are friends to Beta …. . }; Here the class Alpha is the friend class of class Beta i. e. all the member functions of class Alpha will be friend of class Beta.

1. Assignment Questions WAP for Student class which takes 5 students with following properties

1. Assignment Questions WAP for Student class which takes 5 students with following properties Data members: Member function: name, rno, marks[3]. get. Student. Info(), display. Studet. Info(); WAP for Student class which takes 5 students with following properties 1. 2. WAP for Employee class which takes 5 employees with following properties Data members: Member function: name, rno, marks[3]. get. Student. Info(), display. Average(); Static data member: totalsal; Data members: name, rno, sal. Member function: get. Student. Info(), lastupdatedtotalsal() ; // program for calculating the total salary of all employees 2. 3. WAP for Employee class which takes 5 employees with following properties Static data member: totalsal; Data members: name, rno, sal. Member function: get. Student. Info(), static member function : lastupdatedtotalsal() ; // program for calculating the total salary of all employees 4. All above with pointer to object concept. 5. All above using constructor

WAP to implement a class Complex for operations on complex number using constructor with

WAP to implement a class Complex for operations on complex number using constructor with following properties: – Data member: real, img – Member function: add. Complex(), mul. Complex(), disp. Complex()

9. WAP to implement a class Complex for operations on complex number using constructor

9. WAP to implement a class Complex for operations on complex number using constructor with following properties: – Data member: real, img – Member function: add. Complex(), mul. Complex(), disp. Complex() – Copy the state of one object to another using copy constructor 10. WAP to implement a class Rectangle with following properties – Data member: length, breadth – Member function: area. Rectangle() – Friend function: perimeter. Rectangle()

 • WAP to get the details of a project with class Project –

• WAP to get the details of a project with class Project – Data member: pid, pname, ploc, pduration; – Friend function: dispinfo(); – Use constructor for initializing data members