INSTITUTE - UIE
DEPARTMENT- ACADEMIC UNIT-2
Bachelor of Engineering (Computer Science & Engineering)
Subject Name: Object Oriented Programming using C++
Code:22CSH-103
Topic : class DISCOVER . LEARN . EMPOWER
Object Oriented
Programming
using C++
Course Objectives
To enable the students to understand various
stages and constructs of C++ programming
language and relate them to engineering
programming problems.
To improve their ability to analyze and address
variety of problems in programming domains.
2
Course Outcomes
CO Course Outcome
Number
CO1 Understand the concepts of object-oriented programming
including programming process and compilation process.
CO2 Apply different techniques to decompose a problem and
programmed a solution with its sub modules.
CO3 Analyze and explain the behavior of simple programs involving the
programming addressed in the course.
CO4 Implement and evaluate the programs using the syntax and
semantics of object-oriented programming.
CO5 Design the solution of real-world problems in order to determine
that the program performs as expected.
3
Scheme of Evaluation
4
What
. A class is user defined
data type defined in C+
+ using Why
keyword class followed by A class in C++ is the building
the name of class. The block, that leads to Object-
body of class is defined Oriented programming. It is a
inside the curly brackets user-defined data type, which
and terminated by a holds its own data members and
semicolon at the end. member functions, which can be
accessed and used by creating an
instance of that class.
CONTENTS
• Specifying a class
• Creating Objects
• Accessing the class members
6
Class
• A class is user defined data type which consists of two sections, a private and a
protected section that holds data and a public section that holds the interface
operations.
• A class definition is a process of naming a class and data variables, and methods
or interface operations of the class.
• Once a class has been defined, we can create any number of objects belonging to
that class.
• A class is collection of objects of similar type.
Example-- If fruit has been defined as a class, then the statement
fruit mango;
will create an object mango belonging to the class fruit. 7
Specifying A Class
Class specification has two parts:
• Class declaration
• Class function definitions
• Class declaration describes type and scope of its members
• Class function definition describes how class functions are implemented.
Example:
class class_name
{ private: variable declarations;
public: function declaration;
};
8
Objects
• Oops uses object as fundamental Building Block Definitions.
• Objects are the basic run time entities in object oriented System.
• Every object is associated with Data and Functions which define
meaningful operations on an object.
• Object is a real world existing entity.
• Object is an instance of a particular class.
9
Creating Objects
• Objects are created from classes. Class objects are declared in a similar way as
variables are declared. The class name must start, followed by the object name.
For example,
ABC ob1,ob2; //object declaration will create two objects ob1 and
ob2 of ABC class type.
• Memory space is allocated separately to each object for their data members.
• Member variables store different values for different objects of a class.
10
Accessing Class Members
• Accessing a data member depends on the access control of that data
member. If its public, then the data member can be easily accessed
using the direct member access (.) operator with the object of that
class.
• If, the data member is defined as private or protected, then we
cannot access the data variables directly. Then we will have to create
special public member functions to access, use or initialize the private
and protected data members. These member functions are also
called Accessors and Mutator methods or getter and setter functions.
11
Accessing Class Members
class person { //class declaration
public: //access specifier
string name; //variable declaration
int number;
};
main() { //main function
person obj; //object creation for class
cout<<“Enter name:”; //get input values for object variables
cin>>obj.name;
cout<<“Enter number:”;
cin>>obj.number;
cout<<obj.name<<“:”<<obj.number<<endl;
12
Accessing Public Class Members(Example)
Following is an example to show you how to initialize and use the public data members
using the dot (.) operator and the respective object of class.
class Student
{
public:
int rollno;
string name;
};
int main()
{
13
Accessing Public Class Members
Student A;
Student B;
// setting values for A object
A.rollno=2021;
A.name="Karan";
// setting values for B object
B.rollno=20212121;
B.name="Arjun";
cout <<"Name and Roll no of A is: "<< A.name << "-" << A.rollno;
cout <<"Name and Roll no of B is: "<< B.name << "-" << B.rollno;
}
14
OUTPUT:
Name and Roll no of A is: Karan-2021
Name and Roll no of B is: Arjun-20212121
15
Summary
In this lecture we have We have discussed about how
discussed about Class. to specify a class.
Discussed about how to create Discussed about Accessing
Objects. Class members.
16
Frequently Asked question
Q1What is a class?
A class is user defined data type which consists of two sections, a private and a
protected section that holds data and a public section that holds the interface
operations. A class definition is a process of naming a class and data variables, and
methods or interface operations of the class. 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.
Q2 What is an object?
An Object is an instance of a Class. When a class is defined, no memory is allocated but
when it is instantiated (i.e. an object is created) memory is allocated. For example: in real
life, a car is an object.
17
Q3 How to create an object?
Objects are created from classes. Class objects are declared in a
similar way as variables are declared. The class name must start,
followed by the object name.
class name object name;
18
Assessment Questions:
1. Where does the object is created?
A. Class
B. Constructor
C. Destructors
D. Attributes
2.A member function can always access the data in __________ , (in C++).
(A) the class of which it is member
(B) the object of which it is a member
(C) the public part of its class
(D) the private part of its class
19
Discussion forum.
How to create Class and Objects CPP?
How to access class members?
https://youtu.be/6Q0Cff29YwU
https://youtu.be/n9Ej2J0m4a0
20
REFERENCES
Reference Books:
[1] Programming in C++ by Reema Thareja.
[2] Programming in ANSI C++ by E. Balaguruswamy, Tata McGraw Hill.
[3] Programming with C++ (Schaum's Outline Series) by Byron
Gottfried Jitender Chhabra, Tata McGraw Hill.
Websites:
• https://www.tutorialspoint.com/cplusplus/cpp_classes_objects.htm
• https://www.w3schools.com/cpp/cpp_classes.asp
YouTube Links:
• https://youtu.be/6Q0Cff29YwU
• https://youtu.be/n9Ej2J0m4a0
21
THANK YOU