Properties of constructor in c++ with example

Properties of constructor: (1) Constructor is special type function which must has same name of class name. (2) Constructor doesn’t return any data type even void Example: #include #include class school { char *name; int capacity; public: void school(); void display() { cout< #include class math { int a; float b; public: math(int p,float q) //constructor should be in public section { a=p; b=q; } void display() { cout< #include class math { private: int a; float b; math(int p,float q) //private constructor { a=p; b=q; } public: void display() { cout< #include class math { private: int a; float b; math(int p,float q) //private constructor { a=p; b=q; } public: void display() { cout< #include class school { char *name; int capacity; public: school() //constructor inside the class definition { name="DAV"; capacity=500; } void display() { cout< #include class school { char *name; int capacity; public: school(); void display() { cout< #include class school { char *name; int capacity; public: school(); void display() { cout< #include class school { char *name; int capacity; public: friend school(); void display() { cout< #include class school { char *name; int capacity; public: school() //constructor1 { name="DAV"; capacity=500; } school(char *p) //constructor2 { name=p; capacity=500; } school(int q) //constructor 3 { name="DAV"; capacity=q; } school(char *p,int q) //constructor 4 { name=p; capacity=q; } void display() { cout< #include class school { char *name; int capacity; public: school(char *p="KVC",int q=300) { name=p; capacity=q; } void display() { cout<

No comments: