C++-Classes and Objects-2
C++-Classes and Objects-2
M.G.Karthikeyan
NIT-Calicut
Class
most important feature of c++
It is an extension of the idea of structure in C
It is a new way of creating and implementing a
user-defined data type
structure of C
In C a structure can have
only variables as members
In C the structure names are
not standalone and cannot
be used like any other type
names.In other words,the
keyword struct cannot be
omitted in the declaration
of structure variables
struct student s1;
Class
It is a way to bind the data and associated
functions together
It allows the data (and functions) to be hidden
(if necessary) from external use
When defining a class, we are creating a new
abstract data type that can be treated like any
other built-in data type
Class Specification
2- parts
Class declaration
--describes the type and scope of it`s members
Class function definition
Class declaration
similar to struct
declaration
class body contains
declaration of variables
and functions
these functions and
variables are collectively
called as class members
every class member has a
visibility level : public,
private
class class_name
{
visibility level:
variable declaration
function declaration
visibility level:
variable declaration
function declaration
};
Class declaration
class class_name
{
visibility level:
variable declaration
function declaration
visibility level:
variable declaration
function declaration
}
class item
{
private:
int number;
float cost;
public:
void putdata(void);
};
Class declaration
class item
{
private:
int number;
float cost;
public:
void getdata(int a,float b);
void putdata(void);
};
Class declaration
class item
{
int number;
float cost;
public:
void getdata(int a,float b);
void putdata(void);
};
Class declaration
data members:
variables declared
inside the class
member functions:
functions declared
inside the class
data members: number
, cost
member functions:
getdata, putdata
class item
{
int number;
float cost;
public:
void getdata(int a,float b);
void putdata(void);
};
class item
{
int number;
float cost;
public:
void getdata(int a,float b);
void putdata(void)
{
cout<<number; cout<<cost;
}
};
void item::getdata(int a, float b)
{
number = a; cost = b;
}
class item
{
int number;
float cost;
public:
void getdata(int a,float b);
void putdata(void)
{
cout<<number; cout<<cost;
}
};
inline void item::getdata(int a, float
b)
{
number = a; cost = b;
}
Objects
class item
{
int number;
float cost;
public:
void getdata(int a,float b);
void putdata(void)
{
cout<<number; cout<<cost;
}
};
void item::getdata(int a, float b)
{
number = a; cost = b;
}
int main{ item i1;}
Creating objects
declaration of a class defines what would be the
contents of the object that are created out of this
class (if they are created)
declaration of variables of this class creates
objects out of this class
declaration of a class does not allot any memory
space to hold data for classs data members
(since class only gives a template)
declaration of variables of this class(i.e creating
objects of this class) allots memory space to hold
data for object`s data members
Creating objects
class item
{
int number;
float cost;
public:
void getdata(int a,float b);
void putdata(void)
{
cout<<number; cout<<cost;
}
};
void item::getdata(int a, float b)
{
number = a; cost = b;
}
int main{ item i1;item i2;}
Full example
class item
{
int number;
float cost;
public:
void getdata(int a,float b);
void putdata(void)
{
cout<<number; cout<<cost;
}
};
void item::getdata(int a, float b)
{
number = a; cost = b;
}
int main()
{
item i1;
i1.getdata();
i1.putdata();
}
class item
{
int number;
float cost;
somefunction();
public:
void getdata(int a,float b);
};
void item::getdata(int a, float b)
{
number = a; cost = b;
}
void item::somefunction()
{ cout<<hai; }
class trade
{
int base_price;
int price;
int margin;
int no_of_items;
int calculate_price();
public:
get_price(int a);
};
void trade::get_price(int a)
{
margin = 10;
no_of_items = a;
price = calculate_price() + margin;
}
int trade::calculate_price()
{
int temp;
base_price = 100;
temp = 100 + no_of_items;
return temp;
}
now calculate_price has to be a private
member function
class item
{
int number;
float cost;
public:
void getdata(int a,float b);
void putdata(void);
};
class item
{
int number_1;
float cost;
public:
int number_2;
void somefun(int a);
{
number_1 = a;
}
};
int main()
{
item i1;
number_2 = 10;
}
This is not allowed since
number_2 is a public data
member of item
public data members can
be accessed outside the
class definition using the
dot operator only
class item
{
int number_1;
float cost;
public:
int number_2;
void somefun(int a);
{
number_1 = a;
}
};
int main()
{
item i1;
i1.number_2 = 10;
}
This is allowed since
number_2 is a public data
member of item
public data members can
be accessed outside the
class definition using the
dot operator
class item
{
int number_1;
float cost;
public:
int number_2;
void somefun(int a);
{
number_1 = a;
}
};
int main()
{
item i1;
number_1 = 10;
}
This is not allowed since
number_1 is a private data
member of item
private data members
cannot be accessed outside
the class definition (but can
they be accessed using the
dot operator ?)
class item
{
int number_1;
float cost;
public:
int number_2;
void somefun(int a);
{
number_1 = a;
}
};
int main()
{
item i1;
i1.number_1 = 10;
}
This is not allowed since
number_1 is a private data
member of item
private data members
cannot be accessed outside
the class definition even
with dot operator
class item
{
int number_1;
float cost;
public:
int number_2;
void somefun(int a);
{
number_2 = a;
}
};
int main()
{
item i1;
somefun(10);
}
This is not allowed since
somefun is a public
member function of item
and it can be called only
using the dot operator.
class item
{
int number_1;
float cost;
public:
int number_2;
void somefun(int a);
{
number_2 = a;
}
};
int main()
{
item i1;
i1.somefun(10);
}
This is allowed since
somefun is a public
member function of item
and it has been called
using the dot operator.
class item
{
int number_1;
float cost;
public:
int number_2;
void somefun_1(int a);
{
number_1 = a;
}
void somefun_2(int a);
};
void somefun_2(int a);
{ number_1 = a; somefun_1(a); }
int main()
{
item i1;
i1.somefun_2(10);
}
here somefun_1 has been called
without using the dot operator and
this is allowed
because the function call to the
public member function somefun_1
is made from the function
somefun_2 which is itself an another
public member function
This means you can make a call to a
member function (public) without
using dot operator if such call is
made from another member function
(public)
class item
{
int number_1;
float cost;
void somefun_1(int a);
{ number_1 = a; }
public:
int number_2;
void somefun_2(int a);
};
void item::somefun_2(int a);
{ number_1 = a; somefun_1(a); }
int main()
{ item i1; i1.somefun_2(10); }
class item
{
int number_1; int number_3;
float cost;
void somefun_3(int a)
{ number_3 = a; }
void somefun_1(int a)
{ number_1 = a; somefun_3(a);}
public:
int number_2;
void somefun_2(int a);
};
void item::somefun_2(int a);
{ number_1 = a; somefun_1(a); }
int main()
{ item i1; i1.somefun_2(10); }
class item
{
int number_1;
float cost;
void somefun_2(int a);
public:
int number_2;
void somefun_1(int a);
{
number_2 = a;
}
};
void somefun_2(int a)
{ number_2 = a; }
int main()
{
item i1;
somefun_2(10);
}
This is not allowed since
somefun_2 is a private
member function of item and
it cannot be called outside the
class definition (but possible
using dot operator ?).
class item
{
int number_1;
float cost;
void somefun_2(int a);
public:
int number_2;
void somefun_1(int a);
{
number_2 = a;
}
};
void somefun_2(int a)
{ number_2 = a; }
int main()
{
item i1;
i1.somefun_2(10);
}
This is not allowed since
somefun_2 is a private
member function of item
and cannot be called
outside the class definition
even using dot operator.
Friend functions
Some facts about friend functions
1.they are functions (unlike random variables)
2.they are not member functions(as of now)
3.they are non-member functions having access
to private members (normally only member
functions have access to private members)
4.they are non-member functions having access
to private members but only using dot operator
(note:member functions access private members
without using dot operators)
class ABC
{
int a1;
int a2;
void fun1(void);
public:
void fun2(void);
}
void ABC::fun1()
{ cout<<fun1;}
void ABC::fun2()
{ cout<<fun2; fun1();}
int main()
{ ABC abc;abc.fun2(); }
void somefun(void)
{ cout<<somefun; }
We can make somefun to
be friend of ABC
For this we need to add a
keyword friend and declare
this in the class definition
class ABC
{
int a1;
int a2;
void fun1(void);
public:
void fun2(void);
friend void somefun(void);
}
void ABC::fun1()
{ cout<<fun1;}
void ABC::fun2()
{ cout<<fun2; fun1();}
int main()
{ ABC abc;abc.fun2(); }
void somefun(void)
{ cout<<somefun; }
now somefun can access
private members of ABC(if
at all needs to)
class ABC
{
int a1;
int a2;
void fun1(void);
public:
void fun2(void);
friend void somefun(ABC abc1);
}
void ABC::fun1()
{ cout<<fun1;}
void ABC::fun2()
{ cout<<fun2; fun1();}
int main()
{ ABC abc;abc.fun2(); }
void somefun(ABC abc1)
{
abc1.fun1();
cout<<somefun;
}
class ABC
{
int a1;
int a2;
void fun1(void);
public:
void fun2(void);
friend void somefun(void);
}
void ABC::fun1()
{ cout<<fun1;}
void ABC::fun2()
{ cout<<fun2; fun1();}
int main()
{ ABC abc;abc.fun2(); }
void somefun(void)
{
ABC abc1;
abc 1.fun1();
cout<<somefun;
}
class student
{
int no;
char *name;
public:
void getdata(void);
void showdata(void);
void undertaking(void);
};
void student::undertaking()
{
cout<<I will obey rules and
regulations of the institution;
}
int main()
{
student s1;
s1.getdata();
s1.showdata();
s1.undertaking();
}
consider now if I want to display the
undertaking, the student has to take
before enrollment
the undertaking is available under the
function undertaking()
but this can be invoked only using an
object of class student
this object will be created only after the
enrollment ==> solution?
solution : reproduce the function as a
non-member function
int main()
{
void undertaking(void);
undertaking();
student s1;
s1.getdata();
s1.showdata();
s1.undertaking();
}
void undertaking()
{
cout<<I will obey rules and
regulations of the institution;
}
int main()
{
void undertaking(void);
undertaking();
student s1;
s1.getdata();
s1.showdata();
s1.undertaking();
}
void undertaking()
{
cout<<I will obey rules and
regulations of the institution;
}
+
in doing so can you add more functionality to
the function?
but what if you want to do so?
solution : reproduce the function as a nonmember function + make minor modifications
class student
{
int no;
char *name;
int room_no;
public:
void getdata(void);
void showdata(void);
void allotroom(void);
};
void student::allotroom()
{
if(room_no==0) {room_no=-1;}
room_no = room_no + 2;
}
int main()
{
student s1;
s1.getdata();
s1.showdata();
s1.allotroom();
}
now you want the member function
allotroom() to be accessed outside
the class definition
consider you want to do it so that
you can allot room for students as
well as non-students
class student
{
int no;
char *name;
int room_no;
public:
void getdata(void);
void showdata(void);
void allotroom(void);
};
void student::allotroom()
{
if(room_no==0) {room_no=-1;}
room_no = room_no + 2;
}
int main()
{
int role; [0 student, 1 non-student]
cin>>role;
if (role==0)
{
student s1;
s1.getdata();
s1.showdata();
s1.allotroom();
}
else
{ allotroom(); }
}
now we have to reproduce the
function
class student
{
int no;
char *name;
int room_no;
public:
void getdata(void);
void showdata(void);
void allotroom(void);
};
void student::allotroom()
{
if(room_no==0) {room_no=-1;}
room_no = room_no + 2;
}
int main()
{ void allotroom(void); int rm_no;
int role; [0 student, 1 non-student]
cin>>role;
if (role==0)
{
student s1;
s1.getdata();
s1.showdata();
s1.allotroom();
}
else
{ allotroom(); }
}
void allotroom() //slightly different logic
{
if(room_no==0) {room_no=0;}
room_no = room_no + 2;
}
class student
{
int no;
char *name;
int room_no;
public:
void getdata(void);
void showdata(void);
void allotroom(void);
};
void student::allotroom()
{
if(room_no==0) {room_no=-1;}
room_no = room_no + 2;
}
int main()
{ void allotroom(void); int rm_no;
int role; [0 student, 1 non-student]
cin>>role;
if (role==0)
{
student s1;
s1.getdata();
s1.showdata();
s1.allotroom();
}
else
{ allotroom(); }
}
void allotroom() //slightly different logic
{
if(room_no==0) {room_no=0;}
room_no = room_no + 2;
}
class student
{
int no;
char *name;
int room_no;
public:
void getdata(void);
void showdata(void);
void friend allotroom(void);
};
void student::allotroom()
{
if(room_no==0) {room_no=-1;}
room_no = room_no + 2;
}
int main()
{ void allotroom(void); int rm_no;
int role; [0 student, 1 non-student]
cin>>role;
if (role==0)
{
student s1;
s1.getdata();
s1.showdata();
s1.allotroom();
}
else
{ allotroom(); }
}
void allotroom() //slightly different logic
{
if(room_no==0) {room_no=0;}
room_no = room_no + 2;
}
class student
{
int no;
char *name;
int room_no;
public:
void getdata(void);
void showdata(void);
void friend allotroom(student);
};
int main()
{ void allotroom(void); int rm_no;
int role; [0 student, 1 non-student]
cin>>role;
if (role==0)
{
student s1;
s1.getdata();
s1.showdata();
s1.allotroom(s1);
}
else
{allotroom(); }
}
void allotroom(student s1)
{
if(s1.room_no==0) {s1.room_no=0;}
s1.room_no = s1.room_no + 2;
}
class student
{
int no;
char *name;
int room_no;
public:
void getdata(void);
void showdata(void);
void friend allotroom(student,int);
};
int main()
{ void allotroom(void); int rm_no;
int role; [0 student, 1 non-student]
cin>>role;
if (role==0)
{z
student s1;
s1.getdata();
s1.showdata();
s1.allotroom(s1,1);
}
else
{ student s1;allotroom(s1,0); }
}
void allotroom(student s1,int callfor)
{
if(callfor==1)
{
if(s1.room_no==0) {s1.room_no=0;}
s1.room_no = s1.room_no + 2;
}
else
{
if(rm_no==0) {rm_no=-1;}
rm_no = rm_no + 2;
}
}
But the code is not meaningful yet
class student
{
int no;
char *name;
int room_no;
public:
void getdata(void);
void showdata(void);
void friend allotroom(student,int);
};
int main()
{ void allotroom(void); int rm_no;
int role; [0 student, 1 non-student]
while(1){
cin>>role;
if (role==0)
{z
student s1;
s1.getdata();
s1.showdata();
s1.allotroom(s1,1);
}
else
{ student s1;allotroom(s1,0); }
}
}
void allotroom(student s1,int callfor)
{
if(callfor==1)
{
if(s1.room_no==0) {s1.room_no=0;}
s1.room_no = s1.room_no + 2;
}
else
{
if(rm_no==0) {rm_no=-1;}
rm_no = rm_no + 2;
}
}
Friend functions
More facts about friend functions
1.they are functions (unlike random variables)
2.they can be member function of one class and act as
a friend function of another class
3. they can be member function of one class and act as
a friend function of another class. In doing so it can
access the private members of this another class
4. they can be member function of one class and act as
a friend function of another class. In doing so it can
access the private members of this another class but
only using dot operator
class x
{
..
int fun1();
..
};
class y
{
..
frient int x :: fun1();
..
};
Friend class
When all member functions of one class becomes friend functions
of another class,the class having the member functions are called as
friend class
class x
{
..
int fun1();
int fun2();
};
class z
{
friend class x; //all member functions of x are friends to z
}
Friend functions-consolidation
friend functions can be member functions
==>In this case friend functions act as a bridge
between two (or more) classes
friend functions can be non-member functions
==>In this case friend functions act as a bridge
between structured programming part of your
program and object oriented programming part
of your program
when all member functions becomes friend we
get friend class
static members
static data members
static member functions
void somefun(void);
};
int ABC::count; //static variable definition
int main(){ ABC abc; abc.somefun(); }
here static data member is private but static data members
can also be declared as public
class ABC
{
static int count;
int var;
public:
void somefun(void);
};
int ABC::count;
void ABC::somefun(void)
{ cout<<count; count++;}
int main()
{
ABC abc1; ABC abc2;
abc1.somefun();
abc2.somefun();
}
o/p would be,
0
1
total memory:
2 + 2 + 2(for sdm) + fs
class ABC
{
static int count;
int var;
public:
void somefun(void);
};
int ABC::count;
void ABC::somefun(void)
{ cout<<count; count++;}
int main()
{
ABC abc1; ABC abc2;
abc1.somefun();
abc2.somefun();
cout<<ABC :: count;
}
compilation error
class ABC
{
int var;
public:
static int count;
void somefun(void);
};
int ABC::count;
void ABC::somefun(void)
{ cout<<count; count++;}
int main()
{
ABC abc1; ABC abc2;
abc1.somefun();
abc2.somefun();
cout<<ABC :: count;
}
o/p would be
0
1
2
class ABC
{
int a;
public:
static void fun1();
};
void ABC::fun1()
{ cout<<hai; }
int main()
{
ABC::fun1();
}
class ABC
{
int a;
static void fun2();
public:
static void fun1();
};
void ABC::fun1()
{ cout<<hai; }
void ABC::fun2()
{ cout<<hai; }
int main()
{
ABC::fun2();
}
compilation error
because fun2 is private
member and cannot be
accessed outside the class
class ABC
{
int a;
static void fun2();
public:
static void fun1();
};
void ABC::fun1()
{ cout<<hai; fun2(); }
void ABC::fun2()
{ cout<<hai; }
int main()
{
ABC::fun1();
}
no compilation error
class ABC
{
int a;
static void fun2();
public:
static void fun1();
};
void ABC::fun1()
{
cout<<hai;
ABC::fun2();
}
void ABC::fun2()
{ cout<<hai; }
int main()
{
ABC::fun1();
}
no compilation error
class ABC
{
int a;
static int b;
public:
static void fun1();
};
void ABC::fun1()
{ cout<<b; }
int main()
{
ABC::fun1();
}
o/p would be
0
class ABC
{
int a;
static int b;
public:
static void fun1();
};
void ABC::fun1()
{ cout<<a; }
int main()
{
ABC::fun1();
}
compilation error
because a is nonstatic
class ABC
{
int a;
static int b;
public:
static void fun1();
};
void ABC::fun1()
{ cout<<abc.a; }
int main()
{
ABC::fun1();
}
no compilation error
since non-static data
member a is accessed
using dot operator
class ABC
{
int a;
public:
static void fun1();
static void fun2();
};
void ABC::fun1()
{ fun2(); }
void ABC::fun2()
{ cout<<hai; }
int main()
{
ABC::fun1();
}
no compilation error
class ABC
{
int a;
public:
static void fun1();
void fun2();
};
void ABC::fun1()
{ fun2(); }
void ABC::fun2()
{ cout<<hai; }
int main()
{
ABC::fun1();
}
compilation error
because fun2() is nonstatic member function
and it is getting called
from a static-member
function
class ABC
{
int a;
public:
static void fun1();
void fun2();
};
void ABC::fun1()
{ ABC abc;abc.fun2(); }
void ABC::fun2()
{ cout<<hai; }
int main()
{
ABC::fun1();
}
no compilation error
since non-static member
function fun2 is
accessed using dot
operator
Local classes
local classes: classes that are defined inside a
function or block
int a;
public:
void ABC::somefun()
{ a = a + 1; }
int main()
{
ABC abc;
abc.get(1);
}
compilation error
because somefun is a
constant member
function. Hence it cannot
modify data members
pointer to objects
class ABC
{
int a;
public:
void get(int b){ a = b;}
void show(){cout<<a;}
};
int main()
{
ABC abc;
abc.get(1);
ABC *ptr;
ptr->show();
}
o/p would be,
1
pointer to members
class ABC
{
int a;
public:
int b;
void get(int c){ b=c;}
void show(){cout<<b;}
};
int main()
{
ABC abc;
abc.get(1);
class ABC
{
int a;
public:
int b;
void get(int c){ b=c;}
void show(){cout<<b;}
};
int main()
{
ABC abc;
abc.get(1);
END