Download presentation
Presentation is loading. Please wait.
1
CONSTRUCTORS
2
NEED: In C++ Constructors are used to create new objects and assign values to their data members. Constructors are also a member function. Name of a constructor is defined by the system( it is same as the class name). PROPERTIES: Constructors can be overloaded. Constructors has no return type not even void. Its purpose is to construct an object.
3
EXAMPLE: Consider the following class.
class student { private: int roll_no; char name[21]; float marks; public: student(int r,char *s); //constructor 1 void getdata( ); void dispdata( ); }
4
void main( ) { . . . student s1(25,”XYZ”); //using constructor1 for object s1. } s1 Roll_no These private properties are taken up by Name constructors to put into s1.
5
DESIGNING: The constructor defined below initializes the roll no and name. student :: student(int r, char *s) { roll_no = r; strcpy(name,s); } NOTE: Constructor member function is called impicitly. Its only job is to construct an object. It do not process or return anything.
6
Suppose if we pass only one parameter in the main function as shown:
void main( ) { . . . student s2( “ABC”); } Now, this wont work. We need one more constructor. (refferred as constructor2 in the main.) The name will be same as per rule. So we will have functions with same name. This is known as function overloading/polymorphism. So the class will be as follows:
7
class student { private: int roll_no; char name[21]; float marks; public: student(int r,char *s); //constructor 1 student(char *s); //constructor 2 void getdata( ); void dispdata( ); }
8
student :: student(char *s)
{ strcpy( name,s); } Remember that after we add even one constructor then we can’t define an object like this: student s3; // This is invalid. we need to construct one more constructor that do not accept anything. This is illustrated as follows:
9
class student { private: int roll_no; char name[21]; float marks; public: student(int r,char *s); //constructor 1 student(char *s); //constructor 2 student( ); //constructor 3 void getdata( ); void dispdata( ); } student :: student
10
DESTRUCTORS
11
CONCEPT: When we use a constructor to create an object, the program undertakes the responsibility of tracking that object until it expires. That time, the program automatically calls a special member function bearing the formidable title of destructor. The destructor should clean up any debris, so it actually serves a constructive purpose.
12
NOTE: PROPERTIES: No return type not even void.
It is also a member function. same name as class name preceded by a tilde(~) sign. Destructors cannot be overloaded. These are called just before the object expiry. NOTE: When there is no constructor defined then there is a default destructor.
13
EXAMPLE: class student { private: int roll_no; char name[21]; float marks; public: student( ); ~ student( ); void getdata( ); void dispdata( ); }
14
Student :: student( ) { cout<<name<<“bye”; }
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.