Chapter 7 Classes and Objects
Chapter 7 Classes and Objects
Variables Objects
Functions Methods
Syntax: class_object.member_data;
class_object.member_function(arguments);
Chapter 7
Classes and Objects
Members of the class:
Example 1:
class rectangle void display()
{ {
int l,b; cout<<“Area=”<<area;
public: void getdata() }
{ };
cout<<“Enter the length and breadth:\n”; void main()
cin>>l>>b; {
} rectangle r;
void compute_area() r.getdata();
{ r.compute_area();
int area=l*b; r.display();
} }
Chapter 7
Classes and Objects
Members of the class:
Example 2:
#include<iostream.h> void main()
#include<conio.h> {
class data data d1,d2;
{ d1.date(10,6,2021);
int day, month, year; d2.date(11,6,2021);
public: void date(int dd, int mm, int yy) getch();
{ }
day=dd;
month=mm;
year=yy;
cout<<“\t”<<day<<“\t”<<month<<“\t”<<year<<endl;
}
};
Chapter 7
Classes and Objects
Member functions:
- Inside class definition
- Outside class definition
Chapter 7
Classes and Objects
Member function inside class definition:
class rectangle void main()
{ {
int length,breadth; rectangle r;
public: void getdata() r.getdata();
{ r.putdata();
cout<<“Enter the length and breadth:\n”; getch();
cin>>length>>breadth; }
}
void putdata()
{
cout<<“Length=”<<length;
cout<<“Breadth=”<<breadth;
}
};
Chapter 7
Classes and Objects
Member function outside class definition:
Syntax:
return_type classname::memberfunction(arg1, arg2, …, argn)
{
Function body
}
Chapter 7
Classes and Objects
Member function outside class definition:
Example:
#include<iostream.h> int op::prod(int x, int y)
#include<conio.h> {
class op return(x*y);
{ }
public:int sum(int a, int b); void main()
int prod(int x, int y); {
}; op obj;
int op::sum(int a, int b) clrscr();
{ cout<<“Sum=”<<obj.sum(2,3);
return(a+b); cout<<“Product=”<<obj.prod(5,6);
} getch();
}
Chapter 7
Classes and Objects
Member function inside and outside class definition:
class data void data::getdata(int a, float b)
{ {
int number; number=a;
float cost; cost=b;
public: void getdata(int a, float b); }
void putdata() };
{ void main()
cout<<“Number=”<<number<<endl; {
cout<<“Cost=”<<cost; data d;
} d.getdata(10,15.5);
d.putdata();
}
Chapter 7
Classes and Objects
Defining objects of a class:
Syntax:
class user_defined_name
{
private:
public:
};
user_defined_name object1, object2, …;
Chapter 7
Classes and Objects
Defining objects of a class:
Example:
class student
{
int rollno;
char name[20];
char gender;
float per;
public: getdata();
display();
};
student s1,s2;
Chapter 7
Classes and Objects
Arrays as members of classes:
#include<iostream.h> void marks::display()
#include<conio.h> {
class marks cout<<“The marks are:\n”;
{ for(i=0;i<3;i++)
int m[3],i; cout<<m[i]<<endl;
public: void getdata(); }
void display(); void main()
}; {
void marks::getdata() marks m;
{ clrscr();
cout<<"Enter the marks:\n"; m.getdata();
for(i=0;i<3;i++) m.display();
cin>>m[i]; getch();
} }
Chapter 7
Classes and Objects
Array of objects:
class employee
{
char name[20];
int age;
public: void getdata();
void display();
};
employee supervisor[3];
Chapter 7
Classes and Objects
// Program to show the use of array of objects
#include<iostream.h> float data::avg()
#include<conio.h> {
class data return((maths+science)/2.0);
{ }
int rollno, maths, science; void data::putdata()
public: float avg(); {
void getdata(); cout<<"Average="<<avg()<<endl;
void putdata(); }
}; void main()
void data::getdata() {
{ clrscr();
cout<<"Enter the roll number: "; data stud[3];
cin>>rollno; for(int i=0;i<3;i++)
cout<<"Enter maths marks: "; stud[i].getdata();
cin>>maths; getch();
cout<<"Enter science marks: "; }
cin>>science;
putdata();
}
Chapter 7
Classes and Objects
Objects as function arguments:
#include<iostream.h> void main()
#include<conio.h> {
class sum sum s;
{ clrscr();
int a,b; s.getdata();
public: void getdata() s.putdata(s,s);
{ getch();
cout<<"Enter the value of a and b:\n"; }
cin>>a>>b;
}
void putdata(sum x, sum y)
{
int c;
c=x.a+y.b;
cout<<"Sum of a and b="<<c;
}
};
Chapter 7
Classes and Objects
Difference between structure and class:
Structure Class
Defined with the keyword struct Defined with the keyword class