2. Contents
5.1 A Sample C++ Program with class
5.2 Access specifies
5.3 Defining Member Functions
5.4 Making an Outside Function Inline
5.5 Nesting of Member Functions
5.6 Private Member Functions
5.7 Arrays within a Class
5.8 Memory Allocation for Objects
5.9 Static Data Members, Static Member
5.10 Functions, Arrays of Objects
5.11 Object as Function Arguments
5.12 Friend Functions, Returning Objects,
5.13 Const member functions
5.14 Pointer to Members, Local Classes
5.15 Object composition & delegation
3/11/2016 2By:-Gourav Kottawar
3. Declaring Class
Syntax –
class class_name
{
private :
variable declaration;
function declaration;
public:
variable declaration;
function declaration;
}
3/11/2016 3By:-Gourav Kottawar
5. Simple class program
Creating objects
Accessing class members
Access specifies
private
public
protector
default - private
5.3 Defining Member Functions
3/11/2016 5By:-Gourav Kottawar
6. 5.4 Making an Outside Function Inline
class item
{
......
......
public:
void getdata(int a,float b);
};
inline void item :: getdata(int a,float b)
{
number=a;
cost=b;
}
3/11/2016 6By:-Gourav Kottawar
7. 5.5 Nesting of Member Functions
#include<iostream.h>
#include<conio.h>
class data
{
int rollno,maths,science;
int avg();
public: void getdata();
void putdata();
};
main()
{
clrscr();
data stud[5];
for(int i=0;i<5;i++)
stud[i].getdata();
}
void data::getdata()
{
cout<<"Please enter rollno:";
cin>>rollno;
cout<<"Please enter maths marks:";
cin>>maths;
cout<<"Please enter science marks:";
cin>>science;
putdata();
}
int data::avg()
{
int a; a=(maths+science)/2;
return a;
}
void data::putdata()
{
cout<<"Average is :"<<avg()<<endl;
}
3/11/2016 7By:-Gourav Kottawar
8. 5.7 Arrays within a Class
const int size=10;
class array
{
int a[size];
public:
void setval(void);
void display(void);
};
3/11/2016 8By:-Gourav Kottawar
9. Ex-
#include<iostream>
#include<string>
using namespace std;
const int val=50;
class ITEM
{
private:
int item_code[val];
int item_price[val];
int count;
public:
void initiliaze();
void get_item();
void display_item();
void display_sum();
void remove();
};
void ITEM::initiliaze()
{
count=0;
}
void ITEM::get_item()
{
cout<<"Enter the Item code == "<<endl;
cin>>item_code[count];
cout<<"Enter the Item cost == "<<endl;
cin>>item_price[count];
count++;
}
void ITEM::display_sum()
{
int sum=0;
for(int i=0; i<count;i++)
{
sum=sum + item_price[i];
}
cout<<"The Total Value Of The Cost Is ==
"<<sum<<endl;
}
3/11/2016 9By:-Gourav Kottawar
11. int main()
{
ITEM order;
order.initiliaze();
int x;
do
{
cout<<"nnYou have the following opton";
cout<<"nEnter the Appropriate number";
cout<<"nnPress 1 for ADD AN ITEMS";
cout<<"nnPress 2 for DISPLAY TOTAL
VALUE";
cout<<"nnPress 3 for DELETE AN
ITEM";
cout<<"nnPress 4 for DISPLAY ALL
ITEMS";
cout<<"nnPress 5 for QUIT";
cout<<"nEnter The Desired Number ==
n";
cin>>x;
switch(x)
{
case 1:
{
order.get_item();
break;
}
case 2:
{
order.display_sum();
break;
}
case 3:
{
order.remove();
break;
}
case 4:
{
order.display_item();
break;
}
3/11/2016 11By:-Gourav Kottawar
13. 5.8 Memory Allocation for Objects
common for all objects
member function 1
member function 2 memory created when
function defined
Object 1 object 2 object 3
Member varible 1 member variable 2 member
variable 1
Member variable 2 member variable 2 member variable 2
memory created
when objects defined
3/11/2016 13By:-Gourav Kottawar
14. 5.9 Static Data Members, Static Member
A data member of a class can be
qualified as static.
The properties of a static member
variable are similar to that of a C static
variable.
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.
3/11/2016 14By:-Gourav Kottawar
15. #include
using namespace std;
class item
{
static int count;
int number;
public:
void getdata(int a)
{
number = a;
count ++;
}
void getcount(void)
{
cout << "Count: ";
cout << count <<"n";
}
};
int item :: count;
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"<<"n";
a.getcount();
b.getcount();
c.getcount();
return 0;
}
3/11/2016 15By:-Gourav Kottawar
16. #include
using namespace std;
class item
{
static int count;
int number;
public:
void getdata(int a)
{
number = a;
count ++;
}
void getcount(void)
{
cout << "Count: ";
cout << count <<"n";
}
};
int item :: count;
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"<<"n";
a.getcount();
b.getcount();
c.getcount();
return 0;
}
The output of
the program
would be:
Count: 0
Count: 0
Count: 0
After reading
data
Count: 3
Count: 3
Count: 3
3/11/2016 16By:-Gourav Kottawar
17. Sharing of a static data
member
3/11/2016 17By:-Gourav Kottawar
18. Static member function
A member function that is declared
static has following properties
1. A static function can have access to
only other static members (functions
or variables) declared in the same
class.
2. A static member function can be
called using the class name (instead
of its objects ) as follows :
class – name :: function – name;3/11/2016 18By:-Gourav Kottawar
19. 3/11/2016 19
class Something
{
private:
static int s_nValue;
public:
static int GetValue() { return s_nValue; }
};
int Something::s_nValue = 1; // initializer
int main()
{
std::cout << Something::GetValue() <<
std::endl;
}
By:-Gourav Kottawar
20. 3/11/2016 20
class IDGenerator
{
private:
static int s_nNextID;
public:
static int GetNextID() { return
s_nNextID++; }
};
// We'll start generating IDs at 1
int IDGenerator::s_nNextID = 1;
int main()
{
for (int i=0; i < 5; i++)
cout << "The next ID is: " <<
IDGenerator::GetNextID() << endl;
return 0;
}
The next ID is: 1
The next ID is: 2
The next ID is: 3
The next ID is: 4
The next ID is: 5
By:-Gourav Kottawar
21. 3/11/2016 21
class IDGenerator
{
private:
static int s_nNextID;
public:
static int GetNextID() { return s_nNextID++; }
};
// We'll start generating IDs at 1
int IDGenerator::s_nNextID = 1;
int main()
{
for (int i=0; i < 5; i++)
cout << "The next ID is: " << IDGenerator::GetNextID() <<
endl;
return 0;
}
By:-Gourav Kottawar
23. 5.11 Object as Function Arguments
Two ways
1. A copy of the entire object is passed
to the function.
i.e. pass by value
2. Only the address of the object is
transferred to the function
i.e. pass by reference
3/11/2016 23By:-Gourav Kottawar
25. 3/11/2016 25
#include <iostream>
using namespace std;
class Complex
{
private: int real;
int imag;
public:
void Read()
{
cout<<"Enter real and imaginary number
respectively:"<<endl; cin>>real>>imag;
}
void Add(Complex comp1,Complex comp2)
{
real=comp1.real+comp2.real;
/* Here, real represents the real data of object c3 because this function is
called using code c3.add(c1,c2); */
imag=comp1.imag+comp2.imag;
/* Here, imag represents the imag data of object c3 because this function
is called using code c3.add(c1,c2); */
}
By:-Gourav Kottawar
28. 3/11/2016 28
#include <iostream>
using namespace std;
class Complex
{
private: int real;
int imag;
public: void Read()
{
cout<<"Enter real and imaginary number respectively:"<<endl;
cin>>real>>imag;
}
Complex Add(Complex comp2)
{
Complex temp;
temp.real=real+comp2.real;
/* Here, real represents the real data of object c1 because this
function is called using code c1.Add(c2) */
temp.imag=imag+comp2.imag;
/* Here, imag represents the imag data of object c1 because this
function is called using code c1.Add(c2) */
return temp; 0;
} By:-Gourav Kottawar
30. 5.12 Friend function
Need
a data is declared as private inside a
class, then it is not accessible from
outside the class.
A function that is not a member or an
external class will not be able to access
the private data.
A programmer may have a situation
where he or she would need to access
private data from non-member functions
and external classes. For handling such
cases, the concept of Friend functions is
a useful tool.
3/11/2016 30By:-Gourav Kottawar
31. 5.12 Friend function
What is a Friend Function?
A friend function is used for accessing
the non-public members of a class.
A class can allow non-
member functions and other classes to
access its own private data, by making
them friends.
Thus, a friend function is an ordinary
function or a member of another class.
3/11/2016 31By:-Gourav Kottawar
32. 5.12 Friend function
General syntax
Class ABC
{
…..
…..
public :
…..
…..
Friend void func1(void);
};
3/11/2016 32By:-Gourav Kottawar
33. 5.12 Friend function
Special Characteristics
The keyword friend is placed only in the function declaration
of the friend function and not in the function definition.
It is not in the scope of the class to which it has been declared
as friend.
It is possible to declare a function as friend in any number of
classes.
When a class is declared as a friend, the friend class has access
to the private data of the class that made this a friend.
A friend function, even though it is not a member function,
would have the rights to access the private members of the
class.
It is possible to declare the friend function as either private or
public without affecting meaning.
The function can be invoked without the use of an object.
It has object as argument.
3/11/2016 33By:-Gourav Kottawar
34. 3/11/2016 34
#include <iostream>
using namespace std;
class exforsys
{
private:
int a,b;
public:
void test()
{
a=100;
b=200;
}
friend int compute(exforsys e1);
//Friend Function
Declaration with keyword friend and
with the object of class exforsys to
which it is friend passed to it
};
int compute(exforsys e1)
{
//Friend Function Definition
which has access to private data
return int(e1.a+e1.b)-5;
}
void main()
{
exforsys e;
e.test();
cout << "The result is:" <<
compute(e);
//Calling of Friend
Function with object as argument.
}
By:-Gourav Kottawar
35. Constant Member Functions
Declaring a member function with
the const keyword specifies that the
function is a "read-only" function that
does not modify the object for which it is
called.
A constantmember function cannot
modify any non-static data members or
call any member functions that aren't
constant.
The const keyword is required in both the
declaration and the definition.
3/11/2016 35By:-Gourav Kottawar
36. Constant Member Functions –
EXclass Date
{
public:
Date( int mn, int dy, int yr );
int getMonth() const; // A
read-only function
void setMonth( int mn ); // A
write function; can't be const
private:
int month;
};
int Date::getMonth() const
{
return month; // Doesn't
modify anything
}
3/11/2016 36
void Date::setMonth( int mn )
{
month = mn; // Modifies data
member
}
int main()
{
Date MyDate( 7, 4, 1998 );
const Date BirthDate( 1, 18, 1953
); MyDate.setMonth( 4 ); // Okay
BirthDate.getMonth(); // Okay
BirthDate.setMonth( 4 ); // C2662
Error
}
By:-Gourav Kottawar
37. 5.14 Pointers to members
Pointers to members allow you to refer to nonstatic
members of class objects.
You cannot use a pointer to member to point to a static
class member because the address of a static member is
not associated with any particular object.
To point to a static class member, you must use a
normal pointer.
You can use pointers to member functions in the same
manner as pointers to functions.
You can compare pointers to member functions, assign
values to them, and use them to call member functions.
Note - a member function does not have the same type
as a nonmember function that has the same number
and type of arguments and the same return type.
3/11/2016 37By:-Gourav Kottawar
38. 5.14 Pointers to members - Ex
#include <iostream>
using namespace std;
class X
{
public:
int a;
void f(int b)
{
cout << "The value of b is
"<< b << endl;
}
};
3/11/2016 38
int main()
{
// declare pointer to data member
int X::*ptiptr = &X::a;
// declare a pointer to member
function
void (X::* ptfptr) (int) = &X::f;
// create an object of class type X
X xobject;
// initialize data member
xobject.*ptiptr = 10;
cout << "The value of a is " <<
xobject.*ptiptr << endl;
// call member function
(xobject.*ptfptr) (20);
}
By:-Gourav Kottawar
39. 5.14 Local Classes
A local class is declared within a function
definition.
Declarations in a local class can only use type
names, enumerations, static variables from the
enclosing scope, as well as external variables and
functions.
Member functions of a local class have to be
defined within their class definition, if they are
defined at all.
As a result, member functions of a local class are
inline functions. Like all member functions, those
defined within the scope of a local class do not need
the keyword inline.
3/11/2016 39By:-Gourav Kottawar
40. 3/11/2016 40
int x; // global variable
void f() // function definition
{
static int y; // static variable y can be used by local class
int x; // auto variable x cannot be used by local class
extern int g(); // extern function g can be used by local class
class local // local class
{
int g() { return x; } // error, local variable x
// cannot be used by g
int h() { return y; } // valid,static variable y
int k() { return ::x; } // valid, global x
int l() { return g(); } // valid, extern function g
};
}
int main()
{
local* z; // error: the class local is not
visible
// ...}
By:-Gourav Kottawar
41. 3/11/2016 41
A local class cannot have static data members.
void f()
{
class local
{
int f(); // error, local class has noninline
// member function
int g() {return 0;} // valid, inline member
function
static int a; // error, static is not allowed for //
local class
int b; // valid, nonstatic variable
};
} // . . .
By:-Gourav Kottawar