1. 21CS382
Department of CSE
11/2/2022 1
MODULE 1
Introduction to Object Oriented Programming: Computer programming background- C++
overview-First C++ Program -Basic C++ syntax, Object Oriented Programming: What is an object,
Classes, methods and messages, abstraction and encapsulation, inheritance, abstract classes,
polymorphism.
Textbook 1: Chapter 1(1.1 to 1.8)
2. 21CS382
Department of CSE
OBJECT-ORIENTED PROGRAMMING AND C++
NEED FOR C++
LIMITATION OF C
• Lack Of Global View
• Not Designed For Reusability
• No Data Protection
• Does Not Provide Strict Data Type Checking
• It Isn't Easy To Read And Understand The Program
3. 21CS382
Department of CSE
PROGRAMMING
• What is Programming?
• Programming is a process to provide computer-based
solutions to real-world problems
• E.g.: Marksheet Printing Program
4. 21CS382
Department of CSE
OBJECT ORIENTED PROGRAMMIMG
o Procedural programming is about writing procedures or functions that perform
operations on the data, while object-oriented programming is about creating objects that
contain both data and functions.
o Object-oriented programming has several advantages over procedural programming:
o OOP is faster and easier to execute
o OOP provides a clear structure for the programs
o OOP helps to keep the C++ code DRY "Don't Repeat Yourself", and makes the code
easier to maintain, modify and debug
o OOP makes it possible to create full reusable applications with less code and shorter
development time
6. 21CS382
Department of CSE
C++ What are Classes and Objects?
Classes and objects are the two main aspects of object-oriented programming.
7. 21CS382
Department of CSE
CLASS
• A class in C++ is the building block that leads to Object-Oriented programming.
• It is a user-defined data type, which holds its own data members and member
functions, which can be accessed and used by creating an instance of that class.
• A C++ class is like a blueprint for an object.
• For Example: Consider the Class of Cars. There may be many cars with different
names and brand but all of them will share some common properties like all of them
will have 4 wheels, Speed Limit, Mileage range etc. So here, Car is the class and
wheels, speed limits, mileage are their properties.
8. 21CS382
Department of CSE
• A Class is a user defined data-type which has data members and member
functions.
• 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 the above example of class Car, the data member will be speed
limit, mileage etc and member functions can be apply brakes, increase
speed etc.
9. 21CS382
Department of CSE
CREATE A CLASS
class MyClass { // The class
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};
10. 21CS382
Department of CSE
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.
OBJECT
int main() {
MyClass myObj; // Create an object of MyClass
// Access attributes and set values
myObj.myNum = 15;
myObj.myString = "Some text";
// Print attribute values
cout << myObj.myNum << "n";
cout << myObj.myString;
return 0;
}
11. 21CS382
Department of CSE
PROBLEM 1:
Create a class named 'Student' with a string variable 'name' and an
integer variable 'roll_no'. Assign the value of roll_no as '2' and that
of name as "John" by creating an object of the class Student.
PRACTICE PROGRAM
13. 21CS382
Department of CSE
PROGRAM 2:
Create a class by name “Box” with attributes length, breadth and height.
Create 2 objects of the class Box by name “Box1” and “Box2” .
Assign the values for length, breadth and height for both objects.
Calculate the volume of the both the box
PRACTICE PROGRAM
14. 21CS382
Department of CSE
#include <iostream>
using namespace std;
class Box {
public: double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box };
int main() {
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
double volume = 0.0; // Store the volume of a box here
// box 1 specification
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;
// box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
// volume of box 1
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Volume of Box2 : " << volume <<endl; return 0; }
SOLUTION
15. 21CS382
Department of CSE
ABSTRACT DATA TYPE
• Abstract Data type (ADT) is a type (or class) for objects whose behaviour is defined
by a set of values and a set of operations.
• The definition of ADT only mentions what operations are to be performed but not how
these operations will be implemented.
• It does not specify how data will be organized in memory and what algorithms will be
used for implementing the operations.
• It is called “abstract” because it gives an implementation-independent view.
• The process of providing only the essentials and hiding the details is known as
abstraction.
17. 21CS382
Department of CSE
#include <iostream>
using namespace std;
class Sum
{
private:
int x, y, z; // private variables
public:
void add()
{
cout<<"Enter two numbers: ";
cin>>x>>y;
z= x+y;
cout<<"Sum of two number is: "<<z<<endl;
}
};
int main()
{
Sum sm;
sm.add();
return 0;
}
ABSTRACTION EXAMPLE
18. 21CS382
Department of CSE
Implementation details of the class are protected from the inadvertent user level errors.
A programmer does not need to write the low level code.
Data Abstraction avoids the code duplication, i.e., programmer does not have to undergo
the same tasks every time to perform the similar operation.
The main aim of the data abstraction is to reuse the code and the proper partitioning of
the code across the classes.
Internal implementation can be changed without affecting the user level code.
Advantages Of Abstraction