Unit 2
Unit 2
Introduction of Class:
An object oriented programming approach is a collection of objects and each object
consists of corresponding data structures and procedures. The program is reusable and
more maintainable. The important aspect in oop is a class which has similar syntax that of
structure.
class: It is a collection of data and member functions that manipulate data. The data components of class
are called data members and f unctions that manipulate the data are called member functions.
It can also called as blue print or prototype that defines the variables and functions
common to all objects of certain kind. It is also known as user defined data type or
ADT(abstract data type) A class is declared by the keyword class.
Syntax: -
class class_name
{
Access specifier :
Variable declarations;
Access specifier :
function declarations;
};
Access Control:
Access specifier or access modifiers are the labels that specify type of access given to
members of a class. These are used for data hiding. These are also called as visibility modes. There
are three types of access specifiers
1.private
2.public
3.protected
1.Private:
If the data members are declared as private access then they cannot be accessed from other
functions outside the class. It can only be accessed by the functions declared within the class. It is
declared by the key word „private‟ .
2.public:
If the data members are declared public access then they can be accessed from other functions out
side the class. It is declared by the key word „public‟ .
3.protected: The access level of protected declaration lies between public and private. This access
specifier is used at the time of inheritance Note:-
If no access specifier is specified then it is treated by default as private
The default access specifier of structure is public where as that of a class is “private”
Examp
le:
class
student
{
private : int
roll;
char
name[
30];
public:
void get_data()
{ cout<<”Enter roll number and
name”: cin>>roll>>name;
}
void put_data()
{
cout<<”Roll number:”<<roll<<endl;
cout<<”Name :”<<name<<endl;
}
};
Object:-Instance of a class is called object.
Syntax: class_name
object_name; Ex:
student s;
Accessing members:-dot operator is used to access members of class
Object-name.function-name(actual arguments);
Ex:
s.get_data();
s.put_data();
Note:
1.If the access specifier is not specified in the class the default access specifier is private
2.All member functions are to be declared as public if not they are not accessible outside the
class. Object:
Instance of a class is called as object.
Syntax:
Class_name object name;
Example:
student s;
in the above example s is the object. It is a real time entity that can be
used Write a program to read data of a student
#include<iostrea
m> using
namespace std;
class student
{
p
ri
v
a
t
e
:
int roll;
char name[20];
public:
void getdata()
#include<iostrea
m.h> int a=5;
void main()
{
int a=1;
cout<<”Local a=”<<a<<endl;
cout<<”Global a=”<<::a<<endl;
}
Class Scope:
Scope resolution operator(::) is used to define a function outside a class.
#include
<iostream>
using namespace
std;
class sample
{
p
u
b
li
c
:
};
int main()
{ sample
obj;
obj.output();
return 0;
}
Output of program:
Function defined outside the class.
void rectangle::get_data()
{ cout<<”Enter Length of
rectangle”; cin>>L;
cout<<”Enter breadth of
rectangle”; cin>>B;
}
int rectangle::area()
{ return
L*
B;
}
in
t
m
ai
n(
)
{
rec
tan
gle
r;
r.get_data();
cout<<”Area of rectangle is”<<r.area();
return 0;
}
INLINE FUNCTIONS:
Definition:
An inline function is a function that is expanded in line when it is invoked. Inline
expansion makes a program run faster because the overhead of a function call and
return is eliminated. It is defined by using key word “inline”
One of the objectives of using functions in a program is to save some memory space, which becomes
C++ has different solution to this problem. To eliminate the cost of calls to small functions, C++
proposes a new feature called inline function.
General Form:
inline function-header
{ function
body;
}
Eg:
#include<iostream.h>
inline float mul(float x,
float y)
{
return (x*y);
}
inline double div(double p, double q)
{ return
(p/q
);
}
in
t
m
ai
n(
)
{
float a=12.345;
float b=9.82;
cout<<mul(a,b)
;
cout<<div(a,b);
return 0;
}
Properties of inline function:
1.Inline function sends request but not a command to compiler
2.Compiler my serve or ignore the request
3.if function has too many lines of code or if it has complicated logic then it is executed
as
normal function
Situations where inline does not work:
A function that is returning value , if it contains switch ,loop or both then it is treated as
normal function.
if a function is not returning any value and it contains a return statement then it is treated as normal
function
If function contains static variables then it is executed as normal function
If the inline function is declared as recursive function then it is executed as normal function.
Memory Allocation for Objects: Memory for objects is allocated when they are declared but not
when class is defined. All objects in a given class uses same member functions. The member
functions are created and placed in memory only once when they are defined in class definition
STATIC CLASS
MEMBERS
It is initialized to zero when the first object of its class is created. No other initialization is
permitted.Only one copy of that member is created for the entire class and is shared by all the objects of that class, no
matter how
many objects are
created.
It is visible only within the class, but its
lifetime is the entire program.
Static data member is defined by keyword
‟
„static Syntax:
Data type class name::static_variable
Name; Ex: int item::count;
#include<iostre
am.h>
#include<conio.
h> class item
{ static int
count;
int
number
;
public:
void getdata(int a)
{
nu
m
be
r=
a;
co
un
t+
+;
}
void getcount()
{
cout<<"count is"<<count;
}
};
int
item::count;//decler
ation int main() {
item a,b,c;
a.getcount();
b.getcount();
c.getcount();
a.getdata(100);
b.getdata(200);
c.getdata(300);
cout<<"After
reading data";
a.getcount();
b.getcount();
c.get
count();
return 0; }
Output:
count is 0
count is 0
count is 0
After
reading
data count
is 3 count is
3 count is 3
#include<iostream.h>
class test
{ int code;
static
int
count;
public:
void setcode()
{
code=
++cou
nt;
}
void showcode()
{
cout<<”object number”<<code;
}
static void showcount()
{
cout<<”count”
<<count;
}
}; int
test::c
ount;
int
main(
)
{ test t1,t2;
t1.setcode()
;
t2.setcode()
;
test::show
count(); test t3;
t3.setcode();
test::showcount()
; t1.showcode();
t2.showcode();
t3.showcode();
return 0; }
Output: count 2
count 3 object
number 1 object
number 2 object
number 3
Arrays of Objects: Arrays of variables of type "class" is known as "Array of objects". An array of
objects is stored inside the memory in the same way as in an ordinary array.
Syntax:
class class_name
{
private:
data_type
members; public:
data_type members;
member functions;
};
Array of objects:
Class_name
object_name[size];
Where size is the size of
array Ex:
Myclass obj[10];
Write a program to initialize array of objects and print them
#include<iostrea
m> using
namespace std;
class MyClass
{
int
a;
publ
ic:
void set(int x)
{
a
=
x
;
}
i
n
t
g
e
t
(
)
{ return
a
;
}
};
in
t
m
ai
n(
)
{
MyClass
obj[5];
for(int
i=0;i<5;i++
)
obj[i].set(i)
; for(int
i=0;i<5;i++
)
cout<<"obj["<<i<<"].get():"<<obj[i].get()<<endl;
}
O
u
t
p
u
t:
obj[0].get():0
obj[1].get():1
obj[2].get():2
obj[3].get():3
obj[4].get():4
a.Pass-by-value – A copy of object (actual object) is sent to function and assigned to the object of
called function (formal object). Both actual and formal copies of objects are stored at different
memory locations. Hence, changes made in formal object are not reflected to actual object. write a
program to
swap values of two objects
write a program to swap values of two objects
#include<iost
ream.h>
using
namespace
std; class
sample2;
class sample1
{
i
n
t
a
;
p
u
b
l
i
c
:
v
o
i
d
g
e
t
d
a
t
a
(
i
n
t
x
)
;
friend void display(sample1 x,sample2 y);
friend void swap(sample1 x,sample2 y);
};
void sample1::getdata(int x)
{
a=x;
}
class sample2
{
i
n
t
b
;
p
u
b
l
i
c
:
void getdata(int x);
friend void display(sample1 x,sample2 y);
};
void sample1::getdata(int x)
{
a=x;
}
class
sampl
e2 {
int b;
publi
c:
void getdata(int x); friend void
display(sample1 x,sample2 y);
friend void swap(sample1 *x,sample2 *y);
};
void sample2::getdata(int x)
{
b=x
;}
void display(sample1 x,sample2 y)
{ cout<<"Data in object 1
is"<<endl;
cout<<"a="<<x.a<<endl;
cout<<"Data in object 2
is"<<endl;
cout<<"b="<<y.b<<endl;
}
void swap(sample1 *x,sample2 *y)
{
int t;
t=x->a;
x->a=y->b; y-
>b=t; } int
main() {
sample1 obj1;
sample2 obj2;
obj1.getdata(5)
;
obj2.getdata(15);
cout<<"Before Swap of data between Two
objects\n "; display(obj1,obj2);
swap(&obj1,&obj2);
cout<<"after Swap of data between Two objects\n ";
display(obj1,obj2);
}
Before Swap of data between Two objects
Data in object 1 is a=5 Data in
object 2 is b=15 after Swap of
data between Two objects
Data in object 1 is a=15
Data in object 2 is b=5
Ex:
int x=5;
int &y=x;
#include<iost
ream.h>
using
namespace
std; int main()
{ int i=0; int
&j=i; int s=0;
int n;
cout<<"Ent
er n:";
cin>>n;
while(j<=n
)
{
s
=
s
+
i
;
i
+
+
;
}
cout<<"sum="<<s<<endl;
}
Outp
ut:
Enter
n:10
sum=
55
void sample1::getdata(int x)
{
a
=
x
;
}
class sample2
{
i
n
t
b
;
p
u
b
l
i
c
:
void getdata(int x);
friend void display(sample1 x,sample2 y);
friend void swap(sample1 &x,sample2 &y);
};
void sample2::getdata(int x)
{
b
=
x
;
}
void display(sample1 x,sample2 y)
{ cout<<"Data in object 1
is"<<endl;
cout<<"a="<<x.a<<endl;
cout<<"Data in object 2
is"<<endl;
cout<<"b="<<y.b<<endl;
}
void swap(sample1 &x,sample2 &y)
{
int t;
t
=
x
.
a
;
x.a=y.b;
y
.b=t;
} int
main
()
{ sample1
obj1; sample2
obj2;
obj1.getdata(5)
;
obj2.getdata(15);
cout<<"Before Swap of data between Two
objects\n "; display(obj1,obj2);
swap(obj1,obj2);
cout<<"after Swap of data between Two
objects\n "; display(obj1,obj2); }
Output:
Before Swap of data between Two objects
Data in object 1 is
a=5 Data in object 2
is b=15
after Swap of data between Two
objects Data in object 1 is a=15
Data in object 2 is b=5
FRIEND FUNC TIONS:The private members cannot be accessed from outside the class. i.e.…
a non member function cannot have an access to the private data of a class. In C++ a non member
function can access private by making the function friendly to a class.
Definition:
A friend function is a function which is declared within a class and is defined outside the
class. It does not require any scope resolution operator for defining . It can access private members
of a class. It is declared by using keyword “friend” Ex:
class sample
{
i
n
t
x
,
y
;
p
u
b
l
i
c
:
sample(int a,int b);
friend int sum(sample s);
};
sample::sample(int a,int b)
{
x=
a;y
=b
;}
int sum(samples s)
{ int sum;
sum=s.x+s.y
; return 0;
}
void main()
{
Sample
obj(2,3); int
res=sum(ob
j);
cout<< “sum=”<<res<<endl;
}
A friend function possesses certain special characteristics:
It is not in the scope of the class to which it has been declared as friend.
Since it is not in the scope of the class, it cannot be called using the object of that class. It can be
invoked like a normal function without the help of any object.
Unlike member functions, it cannot access the member names directly and has to use an object name
and dot
membership operator with each member name.
It can be declared either in the public or private part of a class without affecting its meaning.
Usually, it has the objects as arguments.
#include<iostream.
h> class sample
{
i
n
t
a
;
i
n
t
b
;
p
u
b
l
i
c
:
void setvalue()
{
a
=
2
5
;
b
=
4
0
;
}
friend float mean(sample s);
};
float mean(sample s)
{ return
float(s.a+s.b)/2.
0;
}
in
t
m
ai
n(
)
{
sa
m
pl
e
X
;
X.setvalue();
cout<<”Mean
value=”<<mean(X);
return 0;
}
write a program to find max of two numbers using friend function for two different
classes
#include<iostream
> using namespace
std; class sample2;
class sample1
{
i
n
t
x
;
p
u
b
l
i
c
:
sample1(int a);
friend void max(sample1 s1,sample2 s2)
};
sample1::sample1(int a)
{
x
=
a
;
}
class sample2
{
i
n
t
y
;
p
u
b
l
i
c
:
sample2(int b);
friend void max(sample1 s1,sample2 s2)
};
Sample2::sample2(int b)
{
y
=
b
;
}
void max(sample1 s1,sample2 s2)
{
If(s1.x>s2.y)
cout<<”Data member in Object of class sample1 is larger
”<<endl; else cout<<”Data member in Object of class sample2 is
larger ”<<endl;
}
void main()
{
sample1
obj1(3);
sample2
obj2(5);
max(obj1,
obj2); }
Friend Class:A class can also be declared to be the friend of some other class. When we create a
friend class then all the member functions of the friend class also become the friend of the other class.
This requires the condition that the friend becoming class must be first declared or defined (forward
declaration).
#include
<iostream.h> class
sample_1
{ friend class sample_2;//declaring friend
class int a,b;
public:
void getdata_1()
{ cout<<"Enter A & B values in class
sample_1"; cin>>a>>b;
}
void display_1()
{
cout<<"A="<<a<<endl;
cout<<"B="<<b<<endl;
}
};
class
sample
_2
{ int c,d,sum;
sample
_1
obj1;
public: void
getdata_
2()
{
obj1.get
data_1();
cout<<"Enter C & D values in class sample_2";
cin>>c>>d;
}
void
sum_
2()
{
sum=obj1.a+obj
1.b+c+d;
}
void display_2()
{
cout<<"A="<<obj1.
a<<endl;
cout<<"B="<<obj1.
b<<endl;
cout<<"C="<<c<<e
ndl;
cout<<"D="<<d<<e
ndl;
cout<<"SUM="<<sum<<endl;
} }; int
main() {
sample_1 s1;
s1.getdata_1()
;
s1.display_1()
;
sample_2 s2;
s2.getdata_2(
);
s2.sum_2();
s2.display_2(
);
}