0% found this document useful (0 votes)
8 views24 pages

L5 - Classes and Objects C++

The document provides an overview of classes and objects in C++, highlighting the differences between structures and classes, such as access specifiers and memory allocation. It explains the declaration and definition of classes, access modifiers (public, private, protected), and introduces nested classes. Additionally, it discusses the internal representation of objects and their memory allocation during creation.

Uploaded by

parishree1906
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)
8 views24 pages

L5 - Classes and Objects C++

The document provides an overview of classes and objects in C++, highlighting the differences between structures and classes, such as access specifiers and memory allocation. It explains the declaration and definition of classes, access modifiers (public, private, protected), and introduces nested classes. Additionally, it discusses the internal representation of objects and their memory allocation during creation.

Uploaded by

parishree1906
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/ 24

JAY PEE IN ST IT U T E O F IN F O R M AT I O N T E C H N O LO GY

(JI IT ), N O ID A
SDF II (15B11CI211)

Lecture 5 – Classes and Objects in C++

1
Structure in C

A structure is a user-defined data type available in C that allows to combining data

items of different kinds. Structures are used to represent a record.

Struct address
{ int houseno;
Char area[20]
Char city[20];
Char state[20];
};
address a1;

2
CLASS
A class is a way to bind data describing an entity and its associated functions together.
class Account
{ int account no;
char type;
float balance;
float deposit (float amount)
{ balance+= amount;
return amount;
}
float withdraw (float amount)
{ balance-= amount;
return amount;
} obj1, obj2
3
Comparative analysis of
Structure and Class
The main difference between structures and classes is that by default, all
member of the structure are public but in class all member are by default
are private.
A structure is considered as the value type whereas, a class is a reference
type.
No structure member can have a null value. Conversely, the variables of a
class can have null values.
In order to initialize the member of a class, the constructors and
destructors are used. but in the structure can initialize its members
automatically.
4
Review of classes and objects

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.
Declaration of class
Data member
Member functions
Program access
Class tag-name
Class definition
Class method definition

6
Definition of class
class class_name
{ access_specifier_1:
member1;
member2;
access_specifier_2:
member3;
member4;
... } object_names;

Where,
class_name is a valid identifier for the class
object_names is an optional list of names for objects of this class.
7
Class Method Definition
Outside the class
Inside the class.
To declare method outside the class we used scope resolution
operator(::)
Syntax: return-type class-name::function name
Example: float Account:: loan()

8
Outside the class
class Account
{ int account no;
char type;
float balance;
};

float Account :: deposit (float amount)


{ balance+= amount;
return amount;
}
float Account :: withdraw (float amount)
{ balance-= amount;
return amount;
}

9
Inside the class
class Account
{ int account no;
char type;
float balance;
float deposit (float amount)
{ balance+= amount;
return amount;
}
float withdraw (float amount)
{ balance-= amount;
return amount;
} obj1, obj2

10
Access Specifier of Class
The Access modifiers of C++ allows us to determine which class members are
accessible to other classes and functions, and which are not.
There are 3 types of access modifiers available in C++:
Public
Private
Protected

11
Public
The data members and member functions declared as public can
be accessed by other classes and functions.
The public members of a class can be accessed from anywhere in
the program using the direct member access operator (.) with the
object of that class.

12
#include <iostream> void sum()
{
using namespace std; int total;
Output
total=num1+num2;
class Test { num1 = 10
cout<<"inside sum";
public: num2=20
cout<<total<<endl;
int num1=10; Total=11
} };
int num2=20;
int main() {
void show() Test obj1,obj2;
{
cout<<"num1"<<num1<<endl; obj1.show();
cout<<"num2"<<num2<<endl; obj2.num1=5;
} obj2.num2=6
obj2.sum();
return 0;
}

13
Private
The class members declared as private can be accessed only by the
member functions inside the class.
They are not allowed to be accessed directly by any object or
function outside the class

14
#include <iostream> public:
void sum() Output
{ error: ‘int Test::num1’ is
using namespace std; private within this
int total;
total=num1+num2; context
class Test {
private: cout<<"inside sum";
int num1=10; cout<<total<<endl;
int num2=20; } };

void show() int main() {


{ Test obj1,obj2;
cout<<"num1"<<num1<<endl; obj1.show();
cout<<"num2"<<num2<<endl; obj2.num1=5;
} obj2.num2=6
obj2.sum();
return 0;
}

15
Protected
Protected access modifier is similar to private access modifier means
it can’t be accessed outside of it’s class unless with the help of friend
class.
This can be implemented through inheritance.(discuss it later)

16
Nested Classes
A nested class is a class which is declared in another class.
The nested class is also a member variable of the enclosing class and
has the same access rights as the other members.

17
class A {
public:
class B {
private:
int num;
public:
void getdata(int n) {
num = n;
}
void putdata() {
cout<<"The number is "<<num;
}
};
};
int main() {
cout<<"Nested classes in C++"<< endl;
A :: B obj;
obj.getdata(9);
obj.putdata();
return 0;
}

18
Internal Representation of
Objects
Object are the instance of a class.
It does not created when we declare class.
We create an object for class, using a class as a type specifier.
For eg
Account a1,a2;
Account is my class name and it has two objects a1 and a2.

19
Memory allocation of objects
Memory is allocated to objects when they are created. And not when the
class is created.
Above statement is partially true.
Member functions are placed in memory only once when they are defined
as a part of class specification.
All objects use same member functions.
Space for member variables is allocated seperately for each object when
they are created.
deposit() withdraw()
Class Account

.
.
. Memory created when member
function are defined

obj1 Obj2

Memory created when objects


declared
101 102
Saving Current
20000 10000

21
What will be the output of
the given program
Output
When the above code is compiled and executed, it produces the following result:
Volume of Box1 : 210
Volume of Box2 : 1560
It is important to note that private and protected members can not be accessed directly using
direct member access operator (.).
References
https://www.geeksforgeeks.org/access-modifiers-in-c/
https://www.techprevue.com/sumita-arora-c-class-12-pdf-solutions/
S. Arora, Computer science in C++. 2002

24

You might also like