mobileNavlogo
headerlogo

Blog

Classes in C++

21 September, 2018

Related Blogs

Classes

Object oriented application is collection of classes. The contents of one class can be used inside another class. Object oriented programming allows to use the contents of one class inside another class using 3 approaches. COMPOSITION (has-a) AGGREGATION (special type of composition)(use-) INHERITANCE(is-a)  

Composition

It is a process of creating an object of one class inside another class. In composition the lifetime of contained object is until container object exist. Contained object is destroyed once container object is deleted.    Example #include<iostream.h> #include<conio.h> class address { char hno[10]; char street[10]; char city[10]; public: void read_address() { cout<<”\n Input hno; cin>>hno; cout<<”\n Input street”; cin>>city ;} void print_address() { cout<<”\n Hno”<<hno; cout<<”\n Street”<< street; cout<<”\n City”<<city; }} class person { char name[10]; address add1; address  add2; public: void read_name() { cout<<”\n Input name”; cin<<name; } void read_address1(); { add1.read_address(); } void read_address2() { add2.read_address(); } void print_name() { cout<<”\n Name”<<name; } void print-address1() { add1.print-address(); } void print-address2() { add2.print-address() } }; void main() { person person1; person11.read-name(); person1.read-address1(); person1.read-address2(); person1.print-name(); person1.print-address1(); person1.print-address2(); } Example class employee  - - >  contain class { int empno; char name[10]; date dob; date  doj; }; class date { int dd,mm,yy; };   Example # include<iostream.h> class date { int dd,MM,YY; public: void read-date() { cout<<”\n Input date”; cin <<dd>>MM>>YY; } void print-date() { cout <<”\n”<<dd<<”\”<<MM”\”<<YY; }}; class employee { char name[10]; date dob; date doj; public : void read-name() { cout<<”\n Input name”; cin >>nam; } void read-dob() { dob.read_date(); } void read-doj() { doj.read-date(); } void print-name() { cout <<”\n Name”<<name; void print-dob() { dob.print-date(); } void print-doj() { doj.print-date(); } }; void main() { clrscr(); employee emp1; emp1.read-name(); emp1.read-doj(); emp1.print-name(); emp1.print-dob(); emp1.print-doj(); }
About Author
Authorlogo
Name

TekSlate

Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills .