0% found this document useful (0 votes)
43 views

C++-Classes and Objects-2

The document discusses classes and objects in C++. It explains that a class defines a new user-defined data type by binding data and functions together. When a class is defined, memory is allocated for member functions, and additional memory is allocated for each object's member variables. Member functions can access private data members, while private members can only be accessed by other member functions. Public members can be accessed from outside the class.

Uploaded by

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

C++-Classes and Objects-2

The document discusses classes and objects in C++. It explains that a class defines a new user-defined data type by binding data and functions together. When a class is defined, memory is allocated for member functions, and additional memory is allocated for each object's member variables. Member functions can access private data members, while private members can only be accessed by other member functions. Public members can be accessed from outside the class.

Uploaded by

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

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 in C vs structure in C++


structure in C++
In C++ a structure can have
variables and functions as
members
In C++ the structure names
are standalone and can be
used like any other type
names.In other words,the
keyword struct can be
omitted in the declaration
of structure variables
student s1;

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

--describes how the class functions are implemented

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 getdata(int a,float b);

void putdata(void);
};

Class declaration

class item
{
private:
int number;
float cost;
public:
void getdata(int a,float b);
void putdata(void);
};

private: can be accessed


from only within the
class
public: can be accessed
from outside the class
also
data hiding (using
private declaration) :key
feature of OOP

Class declaration

class item
{
int number;
float cost;
public:
void getdata(int a,float b);
void putdata(void);
};

use of the private


keyword is optional
By default members of
a class are private

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 function definition

member functions can be defined in 2 places


outside the class defition
inside the class definition
outside the class definition but inline

class function definition outside the


class definition
class item
{
int number;
float cost;
public:
void getdata(int a,float b);
void putdata(void);
};
void item::getdata(int a, float
b)
{
number = a; cost = b;
}

:: tells compiler that the


function getdata belongs
to the class item
:: is called scope
resolution operator
It says that the scope of
the function getdata is
restricted to the class
item

class function definition inside the


class definition

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;
}

When a function is defined inside a


class , it is treated as a inline function

Making an outside function inline

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;
}

here member function definition of


getdata is outside the class definition
we can make it inline using the
qualifier inline in the header line of
function definition
this arrangement is because one of
the objectives of OOP is to separate
the details of implementation from
the class definition

Special characteristics of member


functions
several different classes can use the same
function name. The membership label will
resolve their scope
member functions can access the private data of
the class. A non-member function cannot do
so.(exception to this rule is a friend function)
a member function can call another member
function directly, without using the dot operator
a member function can be called using the dot
operator

Objects

Once a class has been declared we


can create variables of that type
using the class name(like any other
built-in type variable)
This variables is called objects
i1 is the object

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

for n objects memory space alloted


would be,
(n*data members) size + function size
(n*(2+4)) + fs
(2*(2+4)) + fs = 12 + fs (here n=2)

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;}

Classes, objects and memory


memory space for objects is allocated only when they
are declared and not when the class is specified(this
statement is only partly true)
the above statement (partially)says when you specify a
class the memory allocated w.r.t the event of class
specification is 0 (this is totally false)
but class specification and memory allocation are not
totally un-connected parts
i.e when you specify a class the memory allocated w.r.t
the event of class specification is not 0 but >0

Classes, objects and memory


On seeing a class specification, some memory will be
allocated even before any objects are created
this memory allocated is for placing the member
functions of the class in this memory space
again consider the statement when you specify a class
the memory allocated w.r.t the event of class
declaration is not 0 but >0. (This statement is also only
partially true)
the correct statement would be when you declare a
class and define it`s member functions the memory
allocated w.r.t the event of class declaration followed
by the member function definition is not 0 but >0.

Classes, objects and memory


since all the objects belonging to that class
use the same member functions, no separate
space is allocated for member functions when
the objects are created
only space for member variables is allocated
seperately for each object.
separate memory locations for the objects are
essential , because the member variables will
hold different data values for different objects

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();
}

Private member functions


usually data members are
declared as private and the
member functions as public
there may be situations where
member functions has to be
made private instead of public
somefunction is a private
member function

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; }

Private member functions

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

Member access rules


only the member functions can have access to the
private data members and private functions
but the public members(both functions and data) can
be accessed from outside the class
private member function can only be called by another
function that is a member of it`s class
usually data members are declared as private and the
member functions as public
public member function of a class can be called only by
an object of that class using dot operator if the call
happens to be in outside the class definition

Member access rules -1

class item
{
int number;
float cost;
public:
void getdata(int a,float b);
void putdata(void);
};

number and cost can be


accessed by getdata
and putdata
but number and cost
cannot be accessed by
any function outside
the class

Member access rules-2

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

Member access rules-3

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

Member access rules-4

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 ?)

Member access rules-5

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

Member access rules-6

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.

Member access rules-7

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.

Member access rules-8

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)

Member access rules-9

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); }

somefun_1 is a private member


function and somefun_2 is a
public member function
here somefun_1 has been called
without using the dot operator
and this is allowed
because the function call to the
private 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 (private)
without using dot operator if such
call is made from another
member function (public)

Member access rules-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); }

somefun_1 and somefun_3 are


private member functions
here somefun_3 has been called
without using the dot operator and
this is allowed
because the function call to the
private member function somefun_3
is made from the function
somefun_1 which is itself an another
private member function
This means you can make a call to a
member function (private) without
using dot operator if such call is
made from another member function
(private)

Member access rules


you can make a call to a member function
(public or private) without using dot operator
if such call is made from another member
function (public or private)

Member access rules-11

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 ?).

Member access rules-12

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)

Friend functions- Example

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

Friend functions- Example

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)

Friend functions- Example


consider now somefun wants to access private
members of ABC
but it is possible only using dot operators
whereas using dot operators require object
==> first of all we need to create an object
inside somefun
(case-1) void somefun(void) { ABC abc1;abc1.fun1();}
(or)
pass an object as an argument to somefun
(case-2) void somefun(ABC abc1) { abc1.fun1();}

Friend functions- Example (case-1)

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;
}

Friend functions- Example (case-2)

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;
}

Friend functions- Motivating Example


(private members not accessed by friend function)

can a member function be accessed outside


the class definition without dot operator?(no)
but what if you want to do so?
solution : reproduce the function as a nonmember function

Friend functions- Motivating Example


(private members not accessed by friend function)

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

Friend functions- Motivating Example


(private members not accessed by friend function)
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()
{
void undertaking(void);
undertaking();
student s1;
s1.getdata();
s1.showdata();
s1.undertaking();
}
void undertaking()
{
cout<<I will obey rules and
regulations of the institution;
}

Friend functions- Motivating Example


( private members not accessed by friend function)

solution : reproduce the function as a nonmember function


another solution can be arrived at without
reproducing the code
solution using friend function : make the
member function as a non-member function
and declare it to be a friend of the class in
which it was originally a member-function

Friend functions- Motivating Example


(private members not accessed by friend function)
class student
{
int no;
char *name;
public:
void getdata(void);
void showdata(void);
friend void undertaking(void);
};
void student::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;
}

Friend functions- Motivating Example


(private members accessed by friend function)

can a member function be accessed outside


the class definition without dot operator?(no)

+
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

Friend functions- Motivating Example


(private members accessed by friend function)

more specifically solution is,


1.reproduce the member function as a non member function
2.make minor modifications in the nonmember function work so that it`s behaviour
is similar to that of the object`s behaviour
d

Friend functions- Motivating Example


(private members accessed by friend 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()
{
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

Friend functions- Motivating Example


(private members accessed by friend 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()
{
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

Friend functions- Motivating Example


(private members accessed by friend 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;
}

Friend functions- Motivating Example


(private members accessed by friend function)

solution : reproduce the function as a nonmember function + make minor modifications


another solution can be arrived at without
reproducing the code

Friend functions- Motivating Example


(private members accessed by friend function)

solution using friend function :


1.make the member function as a non-member
function
2.declare this non-member function to be the
friend of the class in which it was originally a
member function
3.modify the code of non-member function so
that it works for the objects of the class
4.add code to the non-member function so that it
works for the non-objects

Friend functions- Motivating Example


(private members accessed by friend function) (step-1)

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;
}

Friend functions- Motivating Example


(private members accessed by friend function) (step-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;
}

Friend functions- Motivating Example


(private members accessed by friend function) (step-3)

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;
}

Friend functions- Motivating Example


(private members accessed by friend function) (step-4)

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

Friend functions- Motivating Example


(private members accessed by friend function) (step-4-final code)

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

Friend functions-on more facts

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

static data members


static data members are data members of a
class that are associated with the class rather
than with the object
this means : only one copy of static data
member is created for the entire class and is
shared by all the objects of that class, no
matter how many objects are created
static data members : declaration , definition

static data members-declaration,definition


static data members : declaration is different
from definition
static data members : declared inside the class
and defined outside the class
this is required because static data members
are stored separately rather than as a part of
an object

static data members-declaration,definition


static data members : declared inside the class and
defined outside the class
we can relate such arrangement to member functions.
Member functions are also stored separately rather
than as a part of an object.(similarly) member
functions are declared inside the class and defined
outside the class.The difference is that while in case of
member functions separation of declaration and
definition is not mandatory whereas in case of static
data members separation of declaration and definition
is mandatory

static data members-declaration and


definition
static data members are declared inside the class and
defined outside the class
class ABC
{

static int count; //static variable declaration


public:

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

static data members private,public


non-static data members
data members can be either
public or private
declaration and definition
are same

static data members


static data members can be
either public or private
declaration and definition
are different

static data members private,public


non-static data members
public data members : can be
accessed inside the class
definition without using
object name
public data members : can be
accessed outside the class
definition using object name
public data members : can be
accessed in friend functions
using object name

static data members


public static data members :
can be accessed inside the
class definition without using
object name
public static data members:
can be accessed outside the
class definition using class
name
public static data members :
can be accessed in friend
functions using class name

static data members private,public


non-static data members
private data members : can
be accessed inside the class
definition without using
object name
private data members : can
not be accessed outside the
class definition
public data members : can be
accessed in friend functions
using object name

static data members


private static data members:
can be accessed inside the
class definition without using
object name
private static data members:
cannot be accessed outside
the class definition
private static data members :
can be accessed in friend
functions using class name

accessing static data members why


using class name?
since static data members are associated with
the class rather than with the object they can
be accessed only using class name and not
using object name
for this reason static data members are also
called as class variables

private static data members:


example-1

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

private static data members:


example-2

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

public static data members:


example-1

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

static data members-characterestics


It is initialized to zero when the first object of
it`s class 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 it`s
lifetime is the entire program

need for static data members


they are normally used to maintain values
common to the entire class
For example, an int static data member can
keep a record of no. of objects (of a particular
class) that have been created so for (but for
this you need constructor)

static member functions


data members could be static, similarly
member functions can also be static
static member functions: private,public
static member functions can access only static
data members and static member functions
static member functions can access non-static
data members and non-static member
functions using dot operators

public static member functions-Ex

class ABC
{
int a;
public:
static void fun1();
};
void ABC::fun1()
{ cout<<hai; }

int main()
{
ABC::fun1();
}

private static member functions-Ex-1

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

private static member functions-Ex-2

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

private static member functions-Ex-3

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

static member functions

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

static member functions-Example-1

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

static member functions-Example-2

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

static member functions-Example-3

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

static member functions-Example-4

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

static member functions-Example-5

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

constant member functions


member functions that
cannot modify the object
class ABC
{

int a;
public:

void get(int b){a=b;}

void somefun() const;


}

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);

int A::* ptr;


ptr = &A::b;
cout<<abc.*ptr;
}
o/p would be
1

pointer to objects and 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);

int A::* ptr;


ptr = &A::b;
cout<<abc.*ptr;
ABC *obj_ptr;
obj_ptr = &abc;
abc.get(2);
cout<<obj_ptr->*ptr;
}
o/p would be
1
2

END

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy