Lecture 2.1
Lecture 2.1
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.
8
Objects
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
11
Accessing Class Members
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:
15
Summary
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:
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