1. CLASSES AND
OBJECTS IN C++
by
Mrs. B.Arulmozhi, M.Sc., B.Ed., M.Phil., SET., NET.,
Assistant Professor and Head, Department of Computer
Science and Applications
D.K.M. College for Women, Vellore
2. SYNPOSIS
• Class
• Syntax
• Data Abstraction
• Example
• Creating Object
• Defining member function inside and
outside
• Memory allocation of object
• Example
3. CLASSES
It is a user defined data type.
A class is a way to bind the data and its
associated functions together.
Example:Class student
{
char name[30];
int rollno,m1,m2,tot;
public:void calculate();
};
4. Specifying class:
It has 2 parts.
1. class declaration
2. class function definitions.
Syntax:
Class class-name
{
private: variable-declaration; function-declaration;
public: variable-declaration; function-declaration;
protected: variable-declaration; function-declaration;
}
6. Rules
• class is a keyword
• Body of the class enclosed within braces and
terminated by ;
• Body contains declarations of variables and functions
• It contain 3 access specifiers.(visibility labels)
• private, public and protected
• private members accessed only with in the class.
• public members accessed outside of the class also.
• protected members accessed the members of the
inherted classes only
7. Data abstraction
• The binding of data and functions together into a
single entity is referred to as encapsulation.
• Data hiding is key features of OOPs.
• It can be lead using access specifiers.
•
Private Accessible only its own members
and certain special functions called
as friend functions.
Protected Accessible by members of inherited
classes.
Public Access allowed by other members
in addition to class members and
objects.
8. Data members and Member functions
• Members of the classes are data or functions.
• Variables are declared inside the class is called
data members. In the above ex: name,regno,
m1,m2 and tot are data members.
• Functions are declared inside the class is called
member functions.
• In above ex:accept(), compute(), display(),
execute() and student() are member functions.
9. CREATING OBJECTS
• Object is the instance variable of type class.
• Ex: student stud;
• Here stud is the object of user defined type student
class.
• We access the declared data type of student is lead
using object stud.
• ACCESSING CLASS MEMBERS
• Objectname.functionname();
• Ex: stud.execute();
• Dot operators is used to access the class
members.
10. Ex: using class
#include <iostream.h>
Class add
{
private: int a,b;
public:int sum;
void getdata()
{
a=5;b=6;
sum=a+b;
}
};
void main()
{
add a;//creating object
a.getdata();//access function
cout<<“Sum=”<<s.sum;
}
Output:
Sum =15
11. Ex: defining method outside of the
class
#include <iostream.h>
Class add
{
private: int a,b;
public:int sum;
void getdata();
};
void add::getdata()//outside
{
a=5;b=6;
sum=a+b;
}
void main()
{
add a;//creating object
a.getdata();//access function
cout<<“Sum=”<<s.sum;
}
Output:
Sum =15
Here add:: tells the compiler
that the function belongs to
the class name add.
12. Characteristics of member functions
• several different classes can use the same
function name. The membership label will
resolve the problem.
• Member function can access the private data of
a class. Non-member function cannot access it.
• A member function can call another member
function directly.(nesting of member function)
• Member function can receive the arguments of
a valid c++ data type. Object can also be
passed as arguments.
• A member function can return any valid data
type and also objects.
• A member function can be of static type
13. Memory allocation of Members
• Only one copy of memory allocated for member
functions
• For every objects, separate memory is allocated for
data members
• Ex: add a1,a2;
• 6 bytes for each objects a1 and a2.
• a1 object a2 object methods
a:
b:
Sum:
a:
b:
Sum:
getdata()
{
}
14. Example program- member function
handle arguments
#include<iostream.h>
class product
{
int code,quantity;
float price;
public:
void assign_data(int c,int q,float p)
{
code=c;quantity=q;price=p;
}
void display()
{
cout<<“nCode=“<<code;
cout<<“nQuantity=“<<q
uanity;
cout<<“nCode=“<<price;
}
};
void main()
{
product p;
p.assign_data(101,200,12.5);
p.display();
}
15. Static data members
A data member of a class is qualified as static.
Rules:
• Initialized to zero when the first object of
the class is created.
• No other Initialization is allowed.
• Only one copy of member variable is
created. It is shared by all the other objects
of its class type.
• Its scope or visibility is within the class but
its life time is the lifetime of the program
16. Example program static data members
#include<iostream.h>
class sample
{
int a,b,sum;
static int count;
public:
void accept()
{
cout<<“Enter the value….”;
cin>>a>>b;
sum=a+b;
count++;
}
void display()
{
cout<<“n the sum of 2 no“<<sum;
cout<<“nthis is addition “<<count;
}
};
//static member initialized
out side of the class
int sample.count=0;
void main()
{
sample p1,p2,p3;
p1.accept();
p1.display();
p2.accept();
p2.display();
p3.accept();
p3.display();
}
17. Output:
Enter the values… 2 3
The sum of two numbers 5
This is addition 1
Enter the values… 5 3
The sum of two numbers 8
This is addition 2
Enter the values… 2 5
The sum of two numbers 7
This is addition 3
Rules:
Static member count is
initialized only once
of all the objects of
that class.
It is initialized outside of
the class.
18. Memory allocation of static
Members
• Only one copy of memory allocated static members of
all the classes
• In the above example p1,p2 and p3 objects
• p1 object p2 object p3 object
• static data members
a:2
b:3
Sum:5
a:5
b:3
Sum8:
a:2
b:5
Sum:7
count:3
19. Example program- array of objects
#include<iostream.h>
class product
{
int code,quantity;
float price;
public:
void assign_data(int c,int q,float p)
{
code=c;quantity=q;price=p;
}
void display()
{
cout<<“nCode=“<<code;
cout<<“nQuantity=“<<q
uanity;
cout<<“nCode=“<<price;
}
} p[3];
void main()
{
p[0].assign_data(101,200,12.5);
p[0].display();
}
23. Ex: defining method outside of the
class
#include <iostream.h>
Class add
{
private: int a,b;
public:int sum;
protected: void getdata();
};
void add::getdata()
{
a=b=0;
sum=a+b;
}
void main()
{
a=5;
b=6;
add a1;
a1.getdata();
cout<<“Sum=”<<sum;
}
24. Solution:
Protected member cannot be accessed
outside of the class.
getdata();
Private members cannot be accessed
outside of the class.
a=5
Sum is the public members of class add.
But it is accessed only object of that
class.
a1.sum;
25. Example program static data members
#include<iostream.h>
class sample
{
int a,b,sum;
static int count;
public:
void accept()
{
cout<<“Enter the value….”;
cin>>a>>b;
sum=a+b;
count++;
}
void display()
{
cout<<“n the sum of 2 no“<<sum;
cout<<“nthis is addition “<<count;
}
};
int sample.count=0;
void main()
{
sample p1,p2,p3;
p1.accept();
p1.display();
cout<<p1.count;//error
}