Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
38 views

Chapter 7 Classes and Objects

Uploaded by

Chandana M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Chapter 7 Classes and Objects

Uploaded by

Chandana M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Chapter 7

Classes and Objects


Introduction:

Terminologies used in procedural programming language and object oriented


programming language.

Procedural Programming Object oriented Programming

Variables Objects

User-defined data types Classes

Structure members Instance variables

Functions Methods

Function call Message passing


Chapter 7
Classes and Objects
Definition and declaration of classes and objects:
class user_defined_name
{
private: member data
member functions

public: member data


member functions

protected: member data


member functions
};
Chapter 7
Classes and Objects
Example:
class test
{ void main()
private: int a,b; {
public: void getnum() test t;
{ t.getnum();
cout<<“Enter two numbers for addition:\n”; t.show_data();
cin>>a>>b; getch();
} }
void show_data()
{
cout<<“Addition of two numbers: ”<<a+b;
}
};
Chapter 7
Classes and Objects
Access specifiers:
private: private access means data members can only be accessed by the
member function. Members declared under private are accessible only within
the class. If no access specifier is mentioned, then by default, members are
private.

Example: private: int x;


float y;
Chapter 7
Classes and Objects
Access specifiers:
public: the members under the public can be accessed by any function
outside the class.
Example:
#include<iostream.h> void main()
#include<conio.h> {
class box box obj;
{ clrscr();
int height; obj.length=10;
public: int width, length; obj.width=20;
void set_height(int i) obj.set_height(30);
{ cout<<"Height="<<obj.get_height()<<endl<<"Length="<<obj.length<<endl
height=i; <<"Width="<<obj.width;
} getch();
int get_height() }
{
return height;
}
};
Chapter 7
Classes and Objects
Access specifiers:
protected: the members which are declared using protected can be accessed
only by the member functions, friends of the class and also by the member
functions derived from this class. The member cannot be accessed from
outside. The protected access specifier is therefore similar to private specifier.
Chapter 7
Classes and Objects
//Example for protected member access specifier
#include<iostream.h> double putdata()
#include<conio.h> {
class box return width;
{ }
protected: double width; };
}; void main()
class sbox:box {
{ sbox b;
public: void getdata(double w) b.getdata(5.0);
{ cout<<“Width of box: ”<<b.putdata();
width=w; getch();
} }
Chapter 7
Classes and Objects
Members of the class:
Each member of the class can be accessed with the help of . (dot) operator as
shown below,

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

Members are public by default. Members are private by default.

Contains data members and


Contains only data members.
function members.

You might also like