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

Chapter 5

1. A class is a user-defined data type in C++ that encapsulates data members and member functions, which can be declared as public, private, or protected. 2. An object is an instance of a class that allocates memory dynamically when declared. 3. Classes support data hiding through access specifiers, and binding of data and functions is called encapsulation. Member functions can access private data.

Uploaded by

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

Chapter 5

1. A class is a user-defined data type in C++ that encapsulates data members and member functions, which can be declared as public, private, or protected. 2. An object is an instance of a class that allocates memory dynamically when declared. 3. Classes support data hiding through access specifiers, and binding of data and functions is called encapsulation. Member functions can access private data.

Uploaded by

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

Classes and Objects

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

Here a structure type student is created and a structure


variable “a” is declared of type struct student and assign
values to its different properties.

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.

If the labels are missing, then, by default, all the members


are private.

6
Encapsulation
 The binding of data and functions together into a single
class-type variable is referred to as encapsulation.

Fig: Data hiding in classes 7


Creating Objects
Once a class has been declared, we can create variable of
that type by using the class name. Example:

class item { item x; //memory for x is created


int number; item x, y, z;
public: class item
int getNumber(); {
}; …..
} x, y, z;

Class provides only a template and does not create any


memory space for the objects. Necessary memory space is
allocated to an object when objects are declared or
created. 8
Defining Member Functions
Member functions can be defined in two places:
1. Inside the class definition
2. Outside the class definition
Inside the class definition: class item {
int number;
When a function is defined inside public:
a class, it is treated as inline int getNumber()
{
function. return number;
Only small functions are defined }
};
inside the class definition.

9
Defining Member Functions
Outside the class definition:
return_type class-name :: function-name (argument declaration) {
Function body
};

An important difference between a member function


and a normal function is that a member function
incorporates a membership ‘identity label’ in the
header.
This label tells the compiler which class the function
belongs to.

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

directly, without using the dot operator.

11
Making an Outside function Inline
class item {
int number;
public:
int getNumber();
};

inline int item :: getNumber()


{
return number;
}

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

You might also like