You are on page 1of 50

Class and Object

UNIT-4
By
ADITYA BHARDWAJ

• Email: adityabhardwaj@pec.edu.in • https://www.researchgate.net/pro


file/Aditya_Bhardwaj

1
• Till now we have seen fundamentals of C++
programming.
Objects Name Attributes/ Behaviors/
Properties Functionality/
Actions
Human Beings Age, color, weight, Walking, talking,
etc. sleeping, doing
running etc.

Box Length, breadth, volume, area


height
Mobile Phones Model no., color, Setup call, capture
OS version, RAM photos, play songs,
size, Hard disk size use Internet etc.
First Target
• To learn how we can define basic structure
for concept of class and objects in any
programming language.
Object Oriented Programming
• The main purpose of C++ programming is to add object
orientation to the C programming language and classes
are the central feature of C++ that supports object-
oriented programming and are often called user-defined
types.

• In industries for big projects, we try to solve real-time


problems in the form of virtual world.

• In real world everything is an object. Like in a company


employees are objects.
What Object Will Have ?
• Every object have certain attributes (or properties,
age, height), and behavior (functionality walking,
talking).
• Objects will have something where you can store
data i.e., attributes also called variables, data
members.
• Object will have its own actions i.e., behaviors. To
define behaviors we need to write functions.
Functions in OOPS called as methods or member
functions.
What is Class
• Class is a design or blueprint for the object and
object are real stuff or instance of the class.
Like before constructing building we create
blueprint of the building.
Purpose of Class
• A class is used to specify the design of an
object and it combines data representation
(attributes ) and methods (member functions)
for manipulating that data into one neat package.
• Attributes which are also known as data members.

• The member functions determine the behavior of


the object i.e. provide a definition for supporting
various operations on data held in the form of an
object.
Object Oriented Programming
• To define object’s attribute we need to define
variable, to define behavior we need to define
methods (member functions).

• Attributes of Object-> Data Members (Variables)

• Behavior of Object-> Member Functions


To Summarize
• Data members are the data variables and member
functions are the functions used to manipulate these
variables and together these data members and
member functions defines the properties and behavior
of the objects in a Class.

• In laymen language, design of something is ready in


terms of data members and member functions. Now
these can be accessed by creating an instance of the
class.
Points to Define C++ Class Basic Structure
When you define a class, you define a blueprint for a Object in terms of
attributes and behavior of object it can be design following points:
1. class name: Keyword ‘class’ (class person)
2. Access specifier: What it the scope of data member and function of
object where they can be access in program.
3. How to declare attributes or variables of an object: Declaration of
data members (variables or attributes) of an object. (string name, int
walk)
4. What operations can be performed on such an object: Definition
of member functions. (void running())
5. Come to main function and create an object: Declare an object with
same name as class name. (person player1)
6. How to access data member of object: Using
objectname.datamember (player1.name=“James Bond”)
7. How to access or call member function of object: Using
objectname.memberfunction name (); [player1.running()]
Point 1. C++ Class Definition
• A class is defined in C++ using keyword class followed
by the name of class. The body of class is defined
inside the curly brackets and terminated by a
semicolon at the end.
Point 2. Declare C++ Objects i.e. actual
product manufacturing
• Declaring Objects: A class provides the blueprints
for objects, so basically an object is created from
a class. We declare objects of a class with exactly
the same sort of declaration that we declare
variables of basic types.

Syntax:
• ClassName ObjectName; // Declare object of type
class (eg. car object of type vehicle, vivo phone
object of type mobile)
Point 3. Accessing data members and
member functions:
• The data members and member functions of
class can be accessed using the dot(‘.’)
operator with the object.
• For example if the name of object is obj and
you want to access the member function with
the name running() then you will have to
write obj.running() .
Finally Basic Structure of Class and object
Example 1. Person Object
Observations
• Object data type name is same as class name. It
means object is declared of class datatype.
• By default member function is defined inside
class, but can we define membership function
outside the class ?.
• By default C++ follows private access specifier.
• Data member from class blue print are accessed
using dot (.) operator in main function. Syntax:
Objectname.datamember name
Give Answer for the following
• What is class blueprint in previous question.
• What is data members.
• What is member function.
• Where objects are declared
• Where data members and member functions
are accessed.
2. To Calculate volume of box =
length*breadth*height
Give Answer for the following
• What is class blueprint in previous question.
• What is data members.
• What is member function.
• Where objects are declared
• Where data members and member functions
are accessed.
Example 3. Human Being
4. Concept of C++ Class with Array using Students details Example
Example 5. Lets consider the Scenario of creating a
PECStudent Class

• class PECStudent
• Objects (instances of the class): Student1,
Student2, Student3 etc
• Data members of this class can be Name, Roll
Number, Marks etc.
• Member Functions: Average Marks, Highest
Marks etc
#include <iostream>
#include<cstring> How to access the data members of
using namespace std;
a class inside main function ??
class PECStudent
{
public:
string name;
int studentID;
};

int main() Data Members


{
PECStudent student1; //first object
PECStudent student2; //second object
student1.name=“Raju";
student1.studentID = 123456789;
student2.name="XYZ";
student2.studentID = 987654321;
cout<<"Student1 name is "<<student1.name<<endl;
cout << "student1 id is" << student1.studentID << endl;
cout<<"Student2 name is "<<student2.name<<endl;
cout << "student2 id is" << student2.studentID << endl;
return 0; }
Second Target
Concept of Scope Resolution Operator ::
1. Detailed on Member Functions

• As we know member function of a class is a


function that has its definition or its prototype
within the class definition like any other
variable. It operates on any object of the class
of which it is a member, and has access to all
the members of a class for that object.
• Can we define member function outside
the class definition in C++.
Yes, using Scope resolution operator::
• Yes, Member functions can be defined outside
the class definition using scope resolution
operator.
• Notation for scope resolution operator is
double column : :
• Here, only important point is that you would
have to use class name just before :: operator.
How to implement concept of
Scope resolution operator::
Modification 1, Declaration of member function
in class blueprint.
Example: void running();
Modification 2, Use class name with :: before
member function.
Example: void person::running()
Example 1 With Scope Resolution For Person Object in Highlighted

Change 1- Declaration of member function in design of class;


Change 2- Use class name with :: before member function to implement
program with scope resolution operator.
Example 2 with Scope Resolution For Box
Example 3 with Scope Resolution With Array of Student
Details in Highlighted
Quiz Practice Based on Basic Concepts of

Class and Objects


1. Predict error/output

class A {
public:
int a=3;
void display()
{ cout<<a; } };
int main() {
A a;
a.display();
return 0; }

Output: 3
Memory Allocation for Data Member and
Functions
Once you define class it will not allocate memory space for
the data member of the class. The memory allocation for the
data member of the class is performed separately each time
when an object of the class is created.

Since member functions defined inside class remains same for all
objects, only memory allocation of member function is performed
at the time of defining the class.

Thus memory allocation is performed separately for different


object of the same class. All the data members of each object will
have separate memory space

The memory allocation of class members is shown below:


Memory Allocation
• Hence data member of the class can contain different value for
the different object, memory allocation is performed
separately for each data member for different object at the
time of creating an-object.

Member function remains common for all object. Memory
allocation is done only once for member function at the time of
defining it.
Third Target
C++ Class Access Modifiers
C++ Class Access Modifiers
• The access restriction to the class members is
specified by the labeled public,
private, and protected sections within the class
body. The keywords public, private, and
protected are called access specifiers.
• A class can have multiple public, protected, or
private labeled sections. Each section remains in
effect until either another section label or the
closing right brace of the class body is seen.
• The default access for members and classes
is private.
1. Public Access Modifier
• A public member is accessible from anywhere
outside the class but within a program. You
can set and get the value of public variables
without any member function as shown in the
following example −
Example 1 Public Access Specifier
• When the above code is compiled and
executed, it produces the following result −
2. Private Access Modifier
• A private member variable or function cannot
be accessed, or even viewed from outside the
class. Only the class and friend functions can
access private members.

• By default all the members of a class would be


private.
• For example in the following class width is a private member,
which means until you label a member, it will be assumed a
private member −
Example Private Access Specifier
3. Protected Access Modifier
• A protected member variable or function is
very similar to a private member but it
provided one additional benefit that they can
be accessed in child classes which are called
derived classes.
• In the following example you can check one
derived child class SmallBox from a parent
class Box.
Example Protected Access Specifier
Questions based on Access Specifier
Q1 Questions on Access Specifiers
(Private, Public, Protected)
Output: Error
When no access specifier is
written in class, the data
members and member
functions are assumed as
private.
Now when you call the class by
creating object from main
function, it will give error (as it
will be unable to access the
private data)
Q2
Output: Error

Here we are trying to access private and


protected data of class from main function,
which is not allowed. Only public data can be
accessed.
Q3

It will give output as 12


because both functions of the
class are declared public

You might also like