Chapter 5
Chapter 5
Chapter 5
1
Introduction
The most important feature of C++ is the “Class”.
Stroustrup initially gave the name “C with classes” to
this new language.
A class is an extension of the idea structure used in C.
It is a new way of creating and implementing a user-
defined data type.
2
C structures revisited
Structure is unique feature in C language. Structures
provide a method for packing together data of different
types.
// structure example int main ()
struct student {
{ struct student a;
string name; a.name = “john”;
int roll; a.roll=111;
}; }
3
Limitations of C structures
The standard C does not allow the struct data type to be
treated like built-in types.
For the above structure student if we declare three variable
a, b, and c and assign values to a and b. Then if we write a
statement like c = a + b, will produce an error. Because C
does not permit addition or subtraction of complex
variables.
Another important limitation of C structures is that they
do not permit data hiding. Structure members can be
directly accessed by the structure variables by any function
anywhere in their scope. In other words structure members
are public members.
4
Class & Object
A class is an expanded concept of a data structure:
instead of holding only data, it can hold both data and
functions.
An object is an instantiation of a class. In terms of
variables, a class would be the type, and an object would
be the variable.
class class_name
{
private:
variable declarations;
function declarations;
Class declaration: public:
variable declarations;
function declarations;
};
5
Data Hiding
Data hiding is the key feature of OOP.
1. private members of a class are accessible only from within
other members of the same class.
2. protected members are accessible from members of their
same class, but also from members of their derived classes.
3. Finally, public members are accessible from anywhere
where the object is visible.
6
Encapsulation
The binding of data and functions together into a single
class-type variable is referred to as encapsulation.
9
Defining Member Functions
Outside the class definition:
return_type class-name :: function-name (argument declaration) {
Function body
};
10
Characteristics
of member functions
The member functions have some special
characteristics. These characteristics are:
1. Several different classes can use the same function
name. The membership label will resolve their scope.
2. Member functions can access the private data of the
class. A non-member function cannot do so.
3. A member function can call another member function
11
Making an Outside function Inline
class item {
int number;
public:
int getNumber();
};
12
Nesting of member function
class set { void set :: display ()
int m, n; {
public: cout<<largest()<<endl;
void input(); }
void display();
int largest();
};
int main()
int set :: largest() {
{ set A;
………. A.input();
} A.display();
void set :: input () return 0;
{ }
……….
}
13
Memory Allocation of Objects
Member function are created and
placed in the memory space only
once when they are defined as a
part of class specification.
Since all objects belonging to that
class use the same member
function no separate space is
allocated for member functions
when the objects are created.
14