Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

CLASSES AND OBJECTS

1
Is there any need for “Classes” ??

The 4 pillars of OOP


◼ Abstraction
◼ Encapsulation
◼ Inheritance
◼ Polymorphism
Depends upon the concept of Classes

C++ = C with Classes

2
The concept of a Class
int struct item class item
{ {
int code; int code;
float price; float price;
}; public:
void getdata();
void display();
};
void item :: getdata()
int x; struct item x; {
………..
x=5; x.code = 234; }
cout << x; x.price = 45.50; void item :: display()
{
………..
}

item x;
x.code;
x.putdata();
x.display(); 3
Programming with Classes

Define a Class

Define the member functions

Create objects

Use these objects

4
1. Defining a Class

class class-name class item


{ {
private: private:
variable declaration; int code;
function declaration; float price;
public: public:
variable declaration; void getdata();
function declaration; void display();
}; };

5
2. Defining Member functions
class class_name class item
{ {
private: private:
int code;
variable declaration;
float price;
function declaration; public:
public: void getdata();
variable declaration; void display();
function declaration; };
};
void item :: getdata(void)
{
ret-type class-name :: fn-name (arguments)
cout << “Enter code & price”;
{ cin << code << price;
function body; }
}
void item :: display(void)
{
cout << “code:” << code;
cout << “price:” << price;
}
6
2. Defining Member functions inside the Class

class item
{
private:
int code;
float price;
public:
void getdata();
{
cout << “Enter code & price”;
cin << code << price;
}

void display();
};

void item :: display(void)


{
cout << “code:” << code;
cout << “price:” << price;
} 7
3. Creating Objects and Accessing class Members

class item int main()


{ {
private: item x;
int code;
float price; x.code;
public:
void getdata();
void display(); x.getdata();
}; x.display();

void item :: getdata(void) item y,z;


{
cout << “Enter code & price”;
cin << code << price; y.getdata();
} z.getdata();
y.dispaly();
void item :: display(void) z.dispaly();
{
cout << “code:” << code;
cout << “price:” << price; return 0;
} }
8
A complete C++ program with Class
// Program: ex1.cpp void item :: display(void)
/* Demonstrates the use of Class {
in C++ program */ cout << “\n\t Code = “ << code;
cout << “\n\t Price = “ << price;
# include<iostream.h> }
using namespace std;
/*================================*/
class item {
private: int main()
int code; {
float price;
item x;
public:
x.getdata();
void getdata(void);
x.display();
void display(void);
}; return (0);

/*==============================*/
OUTPUT
void item :: getdata(void){
Enter code: 324
cout << “\n\n Enter code: “;
Enter price: 34.50
cin >> code;
cout <<“\n\t Enter price: “; Code = 324
cin >> price; Price = 34.50 9
}
Another Example with Class
// Program: ex2.cpp
int main()
# include<iostream.h>
{
using namespace std;
person p;
claass person { p.getdata();
p.display();
char name[20]; return (0);
int age;
public:
void getdata(void) //function defination
{
cout << “\n\n Enter name: “;
cin >> name;
cout <<“\n\t Enter age: “;
cin >> age;
}
void display(void);
} OUTPUT
/*=============================================*/
void person :: display(void) Enter name: XYZ
{ Enter age: 26
cout << “\n\t Name = “ << name;
Name = XYZ
cout << “\n\t Age = “ << age;
} Age = 26 10
Nesting Member functions
// Program: ex3.cpp int item :: largest(void)
{
# include<iostream.h> (m>n) ? Return(m) : return(n);
using namespace std; }

void item :: large_display(void)


class set
{
{
cout << “Largest number is:”
int m,n;
<< largest() << “\n”;
public:
}
void input(void);
/*================================*/
int largest(void);
void large_display(void); int main()
}; {
set x;
/*==============================*/ x.input();
x.large_display();
void set :: input(void)
return (0);
{
cout << “\nEnter 2 numbers: “; } OUTPUT
cin >> m >> n;
} Enter 2 numbers: 32 13
Largest number is: 32 11
Making Member functions Private
// Program: ex4.cpp int item :: largest(void)
{
# include<iostream.h> (m>n) ? Return(m) : return(n);
using namespace std; }

class set void item :: large_display(void)


{ {
int m,n; cout << “Largest number is:”
int largest(void); << largest() << “\n”;
public: }
void input(void); /*================================*/
void large_display(void); int main()
}; {
set x;
/*==============================*/
x.input();
void set :: input(void) x.large_display();
{ return (0);
cout << “\nEnter 2 numbers: “; }
cin >> m >> n;
OUTPUT
} Enter 2 numbers: 32 13
Largest number is: 32 12
Arrays of Objects
// Program: ex5.cpp
OUTPUT void employee :: display(void)
{
Details of Manager 1
# include<iostream.h> cout << “\n\t Name = “ << name;
Enter name: XXX
using namespace std; cout << “\n\t Age = “ << age;
Enter age: 34
}
claass employee {
Details of Manager 2 /*================================*/
Enterchar
name: name[20];
YYY
Enterint
age: 43 int main()
age;
{
public:
Details of Manager 3 employee manager[3];
void getdata(void);
Enter name: ZZZ for(int i=1;i<=3;i++)
Enter age: 45void display(void); {
}; cout << “Details of mngr” << i;
Manager 1 manager[i].getdata();
Name: XXX
/*===========================*/ }
Age: 34
for(int i=1;i<=3;i++)
void employee :: getdata(void){ {
Manager 2
Name: XXX<< “\n\n Enter name: “; cout << “Manager” << i;
cout
Age: 43 manager[i].display();
cin >> name; }
cout
Manager 3 <<“\n\t Enter age: “;
return (0);
cin
Name: XXX >> age;
Age: 45 } 13
}
Objects as function arguments
// Program: ex6.cpp
OUTPUT void time :: sum(time t1, time t2)
{
t1 include<iostream.h>
# = 2 hours and 45 minutes minutes = t1.minutes + t2.minutes;
using namespace
t2 = 3 hours and 30std;
minutes hours = minutes/60;
t3 = 6 hours and 15 minutes minutes = minutes%60;
claass time {
hours = hours + t1.hours +t2.hours;
int hours, minutes; }
/*================================*/
public:
void gettime(int h,int m) int main()
{ {
hours = h, minutes = m; time t1, t2, t3;
} t1.gettime(2,45);
void display_time(void) t2.gettime(3,30)
{
cout<<hours<<“hours and”; t3.sum(t1,t2);
cout<<minutes<<“minutes”; cout << “t1 = ” ; t1. display_time();
cout << “t1 = ” ; t1. display_time();
}
cout << “t1 = ” ; t1. display_time();
void sum(time, time); }
};
return (0);
} 14
Static Data members
// Program: ex7.cpp
OUTPUT int main()
{
count:
# 0
include<iostream.h>
item a,b,c;
using
count: 0namespace std; a.getcount();
count: 0
claass item { b.getcount();
c.getcount();
static
After reading dataint count;
count: 3 int number; a.getdata(100);
public:
count: 3 b.getdata(200);
count: 3 void getdata(int a) c.getdata(300);
{
number = a; cout << “After reading data” ;
count++;
} a.getcount();
void getcount(void) b.getcount();
{ c.getcount();
cout << “count:”;
cout << count;
return (0);
}
int item :: count; }
}; 15
Static Member Function
// Program: ex8.cpp
OUTPUT int main()
{
count:
# 2
include<iostream.h>
test t1,t2;
using
count: 3namespace std; t1.setcode();
object no: 1
claass test { t2.setcode();
object no: 2
static
object no: 3 int count; test :: showcount();
int code;
public: test t3;
void setcode(void) t3.setcode();
{
code = ++count;
} test :: showcount();
void showcode(void)
{ t1.showcode();
cout<<“object no:”<<code; t2.showcode();
}
t3.showcode();
static void showcount(void)
{
cout<<“count:”<<count; return (0);
}
}
int test :: count;
16
};
Friend Function
// Program: ex9.cpp void max(XYZ m, ABC n)
{
# include<iostream.h>
if(m.a >=n.a)
using namespace std;
cout << m.a;
Class ABC; // forword declaration else
cout << n.a;
//------------------------------// }
claass XYZ //---------------------------------//
{
int main()
int a; {
public: ABC abc;
void setvalue(int i){x = i;} abc.setvalue(10);
friend void max(XYZ, ABC); XYZ xyz;
}; xyz.setvalue(20);
//------------------------------// max(xyz,abc);

claass ABC return (0);


{ }
int a;
public: OUTPUT
void setvalue(int i){x = i;}
friend void max(XYZ, ABC); 20
}; 17
CONSTRUCTORS
&
DESTRUCTORS

18
Constructors…Is there any need ??
// Program: ex1.cpp void item :: volume(void)
/* Demonstrates the need of {
constructors */ cout << “ The volume of Box = ”;
cout << (width*height*depth);
# include<iostream.h> }
using namespace std;
/*================================*/
class Box {
int main()
double width; {
double height, depth; Box b;
public: b.set_dim();
void set_dim(void); b.volume();
void volume(void); return (0);
};
/*==============================*/

void Box :: set_dim(void){ OUTPUT

cout << “Enter width, height Enter width, height and depth of the Box
and depth of the 2.3 4.1 2.0
Box“;
cin >> width >> height; The volume of Box = 18.86
cin >> depth; 19
}
Constructors…An Example
// Program: ex2.cpp void item :: volume(void)
/* Demonstrates the use of {
constructors */ cout << “ The volume of Box = ”;
cout << (width*height*depth);
# include<iostream.h> }
using namespace std;
/*================================*/
class Box {
int main()
double width; {
double height, depth; Box b;
public: b.volume();
void volume(void); return (0);
};
/*==============================*/

Box :: Box(void)
{ OUTPUT

width = 2.3; Enter width, height and depth of the Box


height = 4.1; 2.3 4.1 2.0
depth = 2.0;
The volume of Box = 18.86
} 20
Parameterized Constructor
// Program: ex3.cpp void item :: volume(void)
/* Demonstrates the use of constructors {
*/ cout << “ \n Volume = ”;
cout << (width*height*depth);
# include<iostream.h> }
using namespace std;
/*================================*/
class Box {
int main()
double width; {
double height, depth; Box B1(2.3, 4.1, 2.0);
public: Box B2(1.0, 1.0, 1.0);
void volume(void); B1.volume();
}; B2.volume();
return (0);
/*==============================*/

Box :: Box(double w, double h, double d)


{ OUTPUT
width = w;
height = h; Volume = 18.86
depth = d; Volume = 1.0
} 21
Inline Constructor
// Program: ex4.cpp void item :: volume(void)
/* Demonstrates the use of constructors {
*/ cout << “ \n Volume = ”;
cout << (width*height*depth);
# include<iostream.h> }
using namespace std;

class Box { /*================================*/

int main()
double width;
{
double height, depth;
Box B1(2.3, 4.1, 2.0);
Box B2(1.0, 1.0, 1.0);
public: B1.volume();
Box :: Box(double w, double h, double d) B2.volume();
{ return (0);
width = w;
height = h;
depth = d;
OUTPUT
}

void volume(void); Volume = 18.86


Volume = 1.0
}; 22
Constructor Overloading
// Program: ex5.cpp Box :: Box(double len)
OUTPUT {
# include<iostream.h>
Volume of B1 = 0
using namespace std; width = height = depth = len;
Volume of B2 = 6.25 }
class Box{
Volume of B3 = 7.5 Box :: Box(double w, double h, double d)
double width; {
double height;
duoble depth; width = w; height = h; depth = d;
public: }
void volume(void) /*================================*/
{
cout << (width*height*depth); int main()
} {
}; Box B1();
Box B2(2.5);
/*==============================*/ Box B3(2.5, 1, 3.0);
cout<<“volume of B1=”<< B1.volume();
Box :: Box() cout<<“volume of B2=”<< B2.volume();
{ cout<<“volume of B3=”<< B3.volume();
return (0);
width = height = depth = 0; }
} 23
Constructor with default argument
// Program: ex6.cpp Box :: Box(double len)
OUTPUT {
# include<iostream.h>
Volume of B1 = 0
using namespace std; width = height = depth = len;
Volume of B2 = 6.25 }
class Box{
Volume of B3 = 7.5 Box :: Box(double w, double h, double d=3.0)
double width; {
double height;
duoble depth; width = w; height = h; depth = d;
public: }
void volume(void) /*================================*/
{
cout << (width*height*depth); int main()
} {
}; Box B1();
Box B2(2.5);
/*==============================*/ Box B3(2.5, 1);
cout<<“volume of B1=”<< B1.volume();
Box :: Box() cout<<“volume of B2=”<< B2.volume();
{ cout<<“volume of B3=”<< B3.volume();
return (0);
width = height = depth = 0; }
} 24
Copy Constructor
// Program: ex7.cpp Box :: Box(double w, double h, double d)
OUTPUT {
# include<iostream.h>
Volume of B1 = 0
using namespace std; width = w; height = h; depth = d;
Volume of B2 = 6.25 }
class Box{
Volume of B3 = 6.25 Box :: Box (Box & B)
double width; {
double height; width = B.width;
duoble depth; height = B.height;
depth = B.depth;
public:
}
void volume(void)
{ /*================================*/
cout << (width*height*depth);
} int main()
}; {
Box B1();
/*==============================*/ Box B2(2.5, 1, 3.0);
Box B3(B2);
Box :: Box()
cout<<“volume of B1=”<< B1.volume();
{ cout<<“volume of B2=”<< B2.volume();
cout<<“volume of B3=”<< B3.volume();
width = height = depth = 0;
return (0);
} } 25
Destructors
// Program: ex8.cpp
OUTPUT
# include<iostream.h> Box :: ~Box()
using namespace
Box object created std; {
cout << “Box Object
class Box{
Volume of B = 7.5 destroyed”;
Box Object destroyed
double width; /*=========================*/
double height;
duoble depth; int main()
public: {
void volume(void) Box B(2.5, 1, 3.0);
{ cout << “volume of B=”;
cout << (width*height*depth); cout << B.volume();
} return (0);
}; }

/*====================================*/
Box :: Box(double w, double h, double d)
{

width = w; height = h; depth = d;


cout << “Box object Created”;
}
26
Assignments

1. Create a class to find the sum of two numbers. This includes two data
members, three member functions to input, output the data members
and to find the sum of two data members.
2. Create a class to find the multiplication of two numbers. This include
three data member of type arrays, three member functions, one is to
accept two matrices, other to display two matrices and 3rd one is to find
the multiplication of two matrices and display the result.
3. Create a struct declaration with a single member function; then create a
definition for that member function. Create an object of your new data
type, and call the member function.
4. Write a class to represent a vector (a series of float values). Include
member functions to perform the following tasks –
• To create the vector
• To modify the value of the given element
• To multiply by a scalar vector
• To display the vector in the form (10,20,30,...).
5. Modify the class and program of previous question such that the
program would be able to add two vector and display the resultant
vector.
6. Exercise 5.1- 5.5 and 6.1 - 6.5 from Balagurusamy

27

You might also like