• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Classes in C++

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();

}

Summary
Review Date
Reviewed Item
Classes in C++
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Classes in C++"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.