0% found this document useful (0 votes)
119 views34 pages

Classes and Objects

1. A class defines the common properties and behaviors of objects. An object is an instance of a class that has specific property values. 2. The class defines properties as data members and behaviors as member functions. Objects are created from classes and can access both data members and member functions. 3. Classes allow for abstraction by grouping common properties and behaviors together without exposing implementation details. Objects hide their internal representations.

Uploaded by

Roshan Shaw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views34 pages

Classes and Objects

1. A class defines the common properties and behaviors of objects. An object is an instance of a class that has specific property values. 2. The class defines properties as data members and behaviors as member functions. Objects are created from classes and can access both data members and member functions. 3. Classes allow for abstraction by grouping common properties and behaviors together without exposing implementation details. Objects hide their internal representations.

Uploaded by

Roshan Shaw
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

CLASSES

&
OBJECTS
1
OBJECT
 An object is a collection of some properties, behavior with
some existence.
 Box object:
Properties:- length , breadth, width. ( data elements)
Behavior:- open, close. ( functions )
Existence:- length=15, breadth=10, width=5 ( data values )

length 15
Data Data
elements values
breadth 10

width 5
A Box object
open()
Functions
close() 2
CLASS
 Class is a collection of some properties, behavior
 BOX class:
Properties:- length , breadth, width. ( data elements)
Behavior:- open, close. ( functions )

length Data
breadth elements

width
open()
Functions
close() 3

Box class
CLASS AND OBJECTS

 Class is a logical structure or prototype whereas Object


has physical existence.
 Object is an instance of a Class.
 Class is a collection of similar types of objects where
the data values may not be the same.
 Class does not poses dynamic properties.

4
PARTS IN A CLASS

class

Member Data Member Function

Class
Class is a keyword in C++ which is used to create user-defined
data-types.They are also known as Abstract data type(ADT) and this5
leads to the feature of Abstraction or Data Abstraction.
Visibility Mode or Access Specifier
The access specifier of a class provides its outside view i.e. it defines
the way, the identifiers (variables and functions) are accessed from
outside.
They are of 3 types:
 public: The identifiers can be accessed directly outside the
class.
 private: The identifiers can only be accessed inside the class.

 protected: same as private but it can be inherited but private


identifiers can’t be.
The default access specifier in a class is private.
 ANY VARIABLE OR FUNCTION SPECIFIED WITH KEYWORD
static ARE CALLED AS CLASS VARIABLE OR CLASS FUNCTIONS.

NOTE !!! 6

Each class definition must be ended with a semicolon mark (;)


GENERAL STRUCTURE OF A CLASS
class class_name NOTE!!!
{ Generally the member
private: data are kept in private
// member variables; area and the member
public: functions in public area of
the class
// member functions;
};

7
AN EXAMPLE(WAY 1)
Class Name
class A{
private: Member Data
int num;
public: Access Specifiers
void get()
Member Functions
cin>>num;
void display()
cout<<num;
};
8
WAY 2
class A{
int num;
public:
void get()
cin>>num;
void display()
cout<<num;
};

If private is not mentioned, then by default it is private 9


CREATION OF OBJECT

 Object is an instance of a class.

Syntax:
<Class_Name> <Object_Name>;

Example:
A obj;

10
ACCESSING MEMBER FUNCTIONS
 The member functions of a class can be accessed by
using dot(.) operator.

Syntax:
<Object_Name>.<Member_Function_Name>;
or
<Object_Name>.<Member_Variable_Name>;

Example:
obj.fun();
11
DEFINING A MEMBER FUNCTIONS
 The member can defined in two ways:
 Within the class definition
 Outside the class definition

Outside the class definition:


Syntax:
<return-type><class_name>::<Member_Function_Name>

12
DEFINE A MEMBER FUNCTION (NON-STATIC)
class Time{
private:
int hour;
int min;
int sec;
public:
void setTime(int h,int m,int s){
hour=h;
min=m; class name
sec=s;
}
member function name
void printTime(void);
};

void Time::printTime(int h, int m, int s){


cout <<hour << “:” << min << “:” << sec << endl;
} 13

Scope-Resolution operator
STRUCTURE VS. CLASS IN C++
• A structure is simply a class whose members are
public by default.
Example: struct Time{
class Time{
int hour;
int hour;
Private by int min; Public by
int min;
default int sec; default
int sec;
public:
public:
void setTime(int,int,int);
void setTime(int,int,int);
void printTime(void)
void printTime(void);
};
};

14
DECLARATION OF AN OBJECT
Similar to declaration of a structure variable in C++.

struct emp e1; emp e1; class Time t1; Time t1;
struct emp e2,e3,e4; emp e2,e3,e4; class Time t2,t3,t4; Time t2,t3,t4;

class Time{
private:
main(){
int hour,min,sec;
public: Time t;
void setTime(int h,int m,int s){ t.setTime(13,27,6);
hour=h; min=m; sec=s;
t.printTime();
}
}
void printTime(void);
}; t hour = 13
void Time::printTime(){ min = 27
cout <<hour << “:” << min << “:” << sec = 6 15
sec << endl;
} 13:27:6
UNDERSTANDING PRIVATE AND PUBLIC
class X{ class X{
class X{
public: int a; int a;
int a; }; public:
}; void set(int b){
a=b;
}
main(){ main(){
void get(){
X x1; X x1;
cout<<a;
x1.a=5; x1.a=5; //Error
cout<<a; //Error cout<<a; //Error }
cout<<x1.a; // 5 cout<<x1.a; //Error };
} }
main(){
X x1;
x1.set(5);
x1.get(); // 5 16
}
MEMBER FUNCTION IN PRIVATE
class X{ main(){
int a;
X x1;
void disp(int b){
a=b; x1.disp(5); // Error
cout<<a; }
}
};

17
PROPERTIES OF MEMBER FUNCTION()
 Several classes can have member function with same name.
 Member function can access all the data members inside the class irrespective
of their access specifiers.
 One member function can call another member function of same class
directly without using dot operator. Such a mechanism is called nested
member function.
 Member functions defined inside the class are inline by default, but the
outside defined member functions are to be made inline explicitly if needed
class X{
int a;
main(){
void disp(int b){
a=b; X x1;
cout<<a; x1.disp(5); // Error
} x1.call_disp(5);
public:
void call_disp(int c){ }
disp(c);
18
}
};
MEMORY ALLOCATION FOR OBJECTS
Common for all objects
Member fun 1

Member fun 2
Memory created when
functions defined.

Member Var 1 Member Var 1 Member Var 1

Member Var 2 Member Var 2 Member Var 2

19
Memory created when
objects created.
THINGS TO REMEMBER …
 When class is defined memory is allocated for member
functions but not for member variables.
 When we declare an object then member variables get
memory allocated.
 Member functions get memory only once.

 All the objects share common member functions.

20
MEMBER FUNCTIONS WITHOUT TAKING
ARGUMENTS
class A void main()
{ {
int a , b; A obj;
public: obj . getData();
void getData() obj.sum();
{ }
cout<<“Enter the values of
a and b”;
cin>>a>>b;
}
void sum()
cout<<a+b;
21
};
MEMBER FUNCTIONS TAKING ARGUMENTS
class A void main()
{ {
int x , y; A obj;
public: obj . getData(7,8);
void getData(int a , int b ) obj.sum();
{ }
x = a;
y = b;
}
void sum()
cout<<x+y;
};
22
MEMBER FUNCTIONS TAKING OBJECTS AS
ARGUMENTS
class A void main()
{ {
int x , y; A ob1,ob2;
public: ob1 . getData(7,8);
void getObjects(A obj1 , A obj2 ) ob2 . getData(4,7);
{ A ob3;
x = obj1.x; ob3.getObjects(ob2,ob1);
y = obj2.y; ob3.getObjects(ob1,ob2);
cout<<x+y<<endl ; } }
void getData(int a , int b )
{
x = a;
y = b;
23
cout<<x+y<<endl; }
};
STATIC MEMBER VARIABLE

 It retains its value throughout the program.


 It gets its memory allocated at the time of class definition.

 Only one copy of the static variable is created and is shared by


all the objects.
 These variables must be initialized by the programmer outside
the class only.
 It can be accessed by class name and :: operator.

24
class X{
main(){
int a;
public: X x1,x2,x3;
static int b; x1.incr(); // 6
void incr(){ x2.incr(); // 7
a=10;
x3.incr(); // 8
b++;
cout<<b; cout<<X::b; // 8
} }
};
int X::b=5; // default ininitalization
// value is zero Static variables are called
as class variables.

Initialization syntax
<data-type> <class name> :: <var-name> = <value>;
25
STATIC MEMBER FUNCTION

 It gets its memory allocated at the time of class definition as like a non-
static member function.
 It can access other static members only (static member variable and other
static member function)
 Like static member variable it can be accessed by class name and ::
operator, hence are called as class function.

Syntax for calling static member functions


<class_name>::<static_member_functions >;
26
class X{ main(){
int a;
function can’t access a
X x1,x2,x3;
Static member
static int b; non-static member directly
x1.incr(); // 1
public:
x2.incr(); // 2
static void incr(){
a=10;
x3.incr(); // 3
b++; X :: incr(); // 4
cout<<X::b;
cout<<b; }
}
};
int X::b;

ERROR: ‘b’ is
private member.

A non-static member function can access both static


27
and non-static members. But, this cannot be directly
accomplished through a static member-function.
class X{ main(){
int a;
X x1;
static int b;
X :: incr(x1); //10 1
public:
}
static void incr(X ob){
ob.a=10;
b++;
cout<<ob.a<<“ ”<<b;
}
};
int X::b;

A static member function can access non-static


members through object of that class.
28
class StaticTest void main()
{ {
public: StaticTest st1,st2,st3,st4;
int x;
static int count; st1.display(20);
void display(int a) st2.display(22);
{ st1.display1( ); //20 2
x=a; st2.display1( ); //22 2
count++;
} st3.display(24);
void display1() st4.display(26);
{ st3.display1( ); //24 4
cout<<x<<“ “<<count<<endl; st4.display1( ) ; //26 4
} }
};
int StaticTest::count; 29
MEMBER FUNCTION OVERLOADING(USING
CLASS)
class X{ main(){
int a,b; X x1,x2,x3;
public:
x1.set();
void set(){
a=b=0;
x1.disp(); // 0 0
} x2.set(5);
void set(int m){ x2.disp(); // 5 5
a=b=m; x3.set(10,20);
}
x3.disp(); // 10 20
void set(int m,int n){
}
a=m;
b=n;
}
void disp(){
cout<<a<<b;
}
31
};
// Member function set() is overloaded
ARRAY OF OBJECTS

class X{ main(){
public:
X p[3];
int a,b;
int i;
};
p[0].a=10;
for(i=0;i<3;i++){
p[0].b=20;
cin>>p[i].a>>p[i].b;
}cout<<p[0].a<<p[0].b;
110 a for(i=0;i<3;i++){
P[0]
112 b p[1].a=15;
cout<<p[i].a<<p[i].b;
114 a }p[1].b=25;
P[1]
116 b } cout<<p[1].a<<p[1].b;
118 a
b P[2] p[2].a=50;
120
p[2].b=60;
cout<<p[2].a<<p[2].b;
} 32
CONST MEMBER FUNCTION

 Does not modify the state of the object

class Time
{
private :
function declaration
int hrs, mins, secs ;

public :

void printTime( ) const ;

}; function definition

void Time :: printTime( ) const


{
33
cout <<hrs << “:” << mins << “:” << secs << endl;
}
POINTER TO AN OBJECT

t2 is a pointer to a Time object


class Time{
private:
main()
int hour,min,sec;
{
public: Time t1;
void setTime(int h,int m,int s){ t1.set(13,27,6); //dot notation
hour=h; min=m; sec=s;
Time *t2;
} t2 = &t1;
void printTime(void); t2->set(8,10,45); //arrow notation
}; }
void Time::printTime(int h, int m, int s){
cout <<hour << “:” << min << “:” <<
sec << endl;
} 5000 t2
t1 6000
hour = 8
13
min = 10
27 5000 ??? 34
sec = 45
6
ASSIGNMENTS
1. Implement a structure Rectangle with following operation
• printValue(): print value of sides rectangle
• setValue(l,w): set the value of sides of rectangle
• area(l,w)
• perimeter(l,w)
2. Create a class COMPLEX to implement the following operations
setNum();
printNum();
add();
subtract();
multiply();
3. Implement Q1 using class by initializing 3 objects in different methods
• Static
• By pointer object
• Dynamic
4. Repeat Q1 and Q3 for Time class with following operations
• printTime()
• setTime(h,m,s)
• inMinutes(): print the time in term of minutes
• inSecond(): print the time interm of seconds 35

You might also like