OOP Lecture 2
OOP Lecture 2
DIAGRAM
CLASS BASIC
• A class is a user-defined type.
• A class consists of a set of members. The most common kinds of members are data members and
member functions.
• Members are accessed using . (dot) for objects and −> (arrow) for pointers.
• Operators, such as +, !, and [], can be defined for a class.
• A class is a namespace containing its members.
• The public members provide the class’s interface and the private members provide
implementation details.
• A struct is a class where members are by default public
DEFI NE A CLASS
• Defining Class data variables (Attributes): A variable inside an object that is part of its state. –
Each object has its own copy of each field
int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}
OUTSIDE CLASS DEFINITION
class MyClass { // The class
public: // Access specifier
void myMethod(); // Method/function declaration
};
void MyClass::myMethod() {
cout << "Hello World!";
}
int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}
EXAMPLE: CAR CLASS
#include <iostream> cout << "Distance driven: " << mileage << " miles"
<< endl;
using namespace std;
}};
class Car {
public:
int main() {
string brand, model;
Car my_car;
int mileage = 0;
my_car.brand = "Honda";
void drive(int distance) {
my_car.model = "Accord";
mileage += distance;
my_car.drive(50);
}
my_car.show_data();
void show_data() {
return 0;
cout << "Brand: " << brand << endl;
}
cout << "Model: " << model << endl;
EXAMPLE:
1. Create a class named Room.
2. Declare variables length, breadth, and height declared inside the class as data members.
3. Define calculateArea() and calculateVolume() are known as member functions of a class.
4. Make object of class Room 5. Access its data member and methods by object created.
ACCESS SPECIFIERS
Access specifiers define how the members (attributes and methods) of a class can be accessed.
• public - members are accessible from outside the class
• private - members cannot be accessed (or viewed) from outside the class
• protected - members cannot be accessed from outside the class, however, they can be accessed in
inherited classes
By default, all members of a class are private if you don't specify an access specifier
ACCESS SPECIFIERS
class MyClass { int main() {
public: // Public access specifier MyClass myObj;
int x; // Public attribute myObj.x = 25; // Allowed (public)
private: // Private access specifier myObj.y = 50; // Not allowed (private)
int y; // Private attribute return 0;
}; }
UML CLASS DIAGRAM
• To describe the static view of a system.
• To show the collaboration among every instance in the static view.
• To describe the functionalities performed by the system.
• To construct the software application using object-oriented languages.
BASIC COMPONENTS OF A CLASS
DIAGRAM
• The standard class diagram is composed of three sections:
• Upper section: Contains the name of the class. This section is always
required, whether you are talking about the classifier or an object.
• Middle section: Contains the attributes of the class. Use this section to
describe the qualities of the class. This is only required when describing
a specific instance of a class.
• Bottom section: Includes class operations (methods). Displayed in list
format, each operation takes up its own line. The operations describe
how a class interacts with data.
CLASS DIAGRAM: CLASSES
• Unidirectional Association:
CLASS DIAGRAM: RELATIONSHIPS
• Dependency
• Unidirectional Association
• Bidirectional Association
CLASS DIAGRAM: RELATIONSHIPS
I N H E R I TA N C E
EXAMPLE:
• Make a UML Class Diagram for Hospital Management System. Consider the following classes:
• Hospital
• Department
• Patient
• Doctor
• User
• Staff
• Ward
• Make a UML Class Diagram for Library Management System
PRINCIPLES OF OOP
• Abstraction – Visibility Controls
• Inheritance – parent child relationships
• Encapsulation – Information Hiding
• Polymorphism – Many Forms
ABSTRACTION IN OOP
• Abstraction refers to the act of representing the crucial requisite features without
including the additional explanations of such features. Hence, it means providing
the end-user with their needs but without the details of how the needs were
fulfilled.
• Hiding implementation details and showing only essential features.
REAL-LIFE EXAMPLE OF
ABSTRACTION
• For instance, we know how the switches at our houses work. Say for the fans, we just simply turn on
the switch if we want to switch it on, or turn off the switch if we want to switch it off. In the case of
fans, we can even use the regulator to adjust the speed according to our needs. Thus, we know all
the features but we don’t know what the background implementation is
• A car's dashboard (we can press the accelerator, but we don’t need to know how the engine works).
• ATM machine (Users just press buttons to withdraw money, but the backend process remains
hidden).
IMPLEMENTATION OF ABSTRACTION
1. Using classes
• A class can be used to group every data member and function into a solo unit with help of access
specifiers.
• A class can determine which data of the class could be accessible to the outside world.
• In this way with the use of access specifiers, classes help to implement abstraction
IMPLEMENTATION OF ABSTRACTION
2. Through header files
• For example, if we put the header file for mathematical operations i.e. <cmath> or <math.h>, at
the start of the program we can use the sqrt() function to find the square root of any given
number.
• In this way, we know what sqrt() does, but we do not know the background details of how it finds
the square root.
ABSTRACTION EXAMPLE
#include <iostream> num = n; ch = c;
using namespace std; }
class AbstractionExample{ void getMyValues() {
private: cout<<"Numbers is: "<<num<< endl;
/* By making these data members private, I have hidden them from outside cout<<"Char is: "<<ch<<endl;
world. These data members are not accessible outside the class. The only way to }};
set and get their values is through the public functions.*/
int main(){
int num;
AbstractionExample obj;
char ch;
obj.setMyValues(100, 'X');
public:
obj.getMyValues();
void setMyValues(int n, char c) {
return 0;}
TYPES OF ABSTRACTION
1- Data Abstraction
When the object data is not visible to the outer world, it creates data abstraction. If needed, access to
the Objects’ data is provided through some methods.
TYPES OF ABSTRACTION
2- Process Abstraction
• We don’t need to provide details about all the functions of an object. When we hide the internal
implementation of the different functions involved in a user operation, it creates process
abstraction.
GENERALIZATION AND SPECIALIZATION
• The greatest thing about object-oriented programming is code reuse. However, making code
reusable isn’t always the easiest to do. You have to use a different mindset when coding.
• One such option is Generalization/Specialization, a term often used in UML
• Generalization/Specialization is taking something general and narrowing it down or starting
specific and going more general (Specialization/Generalization).
GENERALIZATION AND SPECIALIZATION
Generalization
• generalization is a bottom-up method used to combine lower-level entities into a higher-level
object. This approach creates a more generic entity, known as a superclass, by combining entities
with similar features. By removing duplication and arranging the data in a more organized
manner, generalization streamlines the data model.
• Cuts Down on Redundancy
• Simplifies Schema
GENERALIZATION AND SPECIALIZATION
Specialization
• specialization is a top-down method where a higher-level entity is split into two or more lower-
level entities according to their distinct qualities. This technique, which includes splitting a single
entity set into subgroups, is often connected to inheritance, in which attributes from the higher-
level entity are passed down to the lower-level entities.
• Enhances Specificity
• Encourages Inheritance
INHERITANCE
• The capability of a class to derive properties and characteristics from another class is called
Inheritance.
• Inheritance is one of the most important feature of Object Oriented Programming.
• Sub Class: The class that inherits properties from another class is called Sub class or Derived
Class.
• Super Class: The class whose properties are inherited by sub class is called Base Class or Super
class.
THE "IS A" RELATIONSHIP
• Inheritance establishes an "is a" relationship between classes.
– A poodle is a dog
– A car is a vehicle
– A flower is a plant
– A football player is an athlete
INHERITANCE – TERMINOLOGY AND
NOTATION IN C++
• Base class (or parent)
• Derived class (or child) – inherits from the base class
• Notation:
class Student // base class
{
...
};
class UnderGrad : public student
{ // derived class. . .
};
BACK TO THE ‘IS A’ RELATIONSHIP
• An object of a derived class 'is a(n)' object of the base class
• Example:
• An UnderGrad is a Student
• A derived object has all of the characteristics of the base class
WHAT DOES A CHILD HAVE?
An object of the derived class has:
• all members defined in child class
• all members declared in parent class
An object of the derived class can use:
• all public members defined in child class
• all public members defined in parent class
PROTECTED MEMBERS AND CLASS
ACCESS
• protected member access specification: like private, but accessible by objects
of derived class
• Class access specification: determines how private, protected, and public
members of base class are inherited by the derived class
CLASS ACCESS SPECIFIERS
1. public – object of derived class can be treated as object of base class (not vice-versa)
2. protected – more restrictive than public, but allows derived classes to know details of parents
3. private – prevents objects of derived class from being treated as objects of base class
INHERITANCE VS. ACCESS
INHERITANCE VS. ACCESS
INHERITANCE VS. ACCESS
INHERITANCE VS. ACCESS
INHERITANCE EXAMPLE
INHERITANCE EXAMPLE
INHERITANCE SYNTAX
Ex 1:
class B: public A
{ // Members of class B };
In the above syntax, class A is a base class, and class B is a derived class. Here, the class B is
derived publicly.
Ex 2:
class B: private A // private derivation
{ // members of class B };
INHERITANCE SYNTAX
Ex 3:
class B: A // by default private derivation
{ // members of class B };
Ex 4:
class B: protected A
{ // members of class B };
/* C++ PROGRAM TO ACCESS PROTECTED DATA
MEMBER USING INHERITANCE */
class A{ class B: public A{ int main(){
private: private:
int a; int b; B objB;
protected: public: objB.set_a(10);
int p; objB.set_b(20);
void set_b(int b){ objB.set_p(30);
public: this->b=b;}
void set_a(int a){
void set_p(int p){ objB.get_a();
this->a=a; this->p=p;} objB.get_b();
} objB.get_p();
void get_b(){
return 0;
void get_a(){
cout<<"b="<<b<<endl; }
cout<<"a="<<a<<endl; }
} void get_p(){
};
cout<<"p="<<p<<endl; }
};
Lecture
10/24/2023
8
TYPES OF INHERITANCE
• Single Inheritance
• Multiple Inheritance
• Multilevel Inheritance
• Hierarchical Inheritance
• Hybrid Inheritance
Lecture 8
SINGLE INHERITANCE
• Single Inheritance is the most primitive among all the types of inheritance in C++. In this
inheritance, a single class inherits the properties of a base class. All the data members of the base
class are accessed by the derived class according to the visibility mode (i.e., private, protected,
and public) that is specified during the inheritance.
10/24/2023
Lecture 8
SINGLE INHERITANCE
// base class
// main function int main()
class Vehicle {
{ // creating object of sub class will invoke the
public: constructor of base classes
Vehicle()
{ Car obj; return 0;
cout << }
"This is a
Vehicle" << Output:
endl; This is a Vehicle.
}
};
// sub class derived from base class
class Car: public Vehicle
{
};
10/24/2023
Lecture 8
MULTIPLE INHERITANCE
• The inheritance in which a class can inherit or derive the characteristics of multiple classes, or a
derived class can have over one base class, is known as Multiple Inheritance.
• It specifies access specifiers separately for all the base classes at the time of inheritance. The
derived class can derive the joint features of all these classes and the data members of all the base
classes are accessed by the derived or child class according to the access specifiers.
10/24/2023
MULTIPLE INHERITANCE
// sub class derived from
// first base class // second base class
two base classes
class Vehicle class FourWheeler class Car: public Vehicle,
{ public: { public: public FourWheeler {
Vehicle() FourWheeler() };
{ // main function
{ int main()
cout << "This is a 4
cout << "This is a {
Vehicle" wheeler Vehicle" <<
Car obj;
<< endl; endl; return 0;
} } }
}; };
Output:
This is a Vehicle
This is a 4 wheeler Vehicle
Lecture 8
MUTILEVEL INHERITANCE
• The inheritance in which a class can be derived from another derived class is
known as Multilevel Inheritance.
• Suppose there are three classes A, B, and C. A is the base class that derives
from class B. So, B is the derived class of A. Now, C is the class that is
derived from class B. This makes class B, the base class for class C but is the
derived class of class A. This scenario is known as the Multilevel Inheritance.
10/24/2023
Lecture 8
MULTILEVEL INHERITANCE
// base class class fourWheeler: class Car: public fourWheeler
public Vehicle {
class Vehicle
{ public: public:
{ car()
fourWheeler()
public: { cout<<"Car has 4 Wheels"<<endl;
{ cout<<"Objects
Vehicle() with 4 wheels are };
{ cout << "This is a vehicles"<<endl; // main function int main()
Vehicle" << endl; {
}
}; Car obj; return 0;
} }
};
Output:
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels
10/24/2023
Lecture 8
HIERARCHICAL INHERITANCE:
• The inheritance in which a single base class inherits multiple derived classes is known as the
Hierarchical Inheritance.
• This inheritance has a tree-like structure since every class acts as a base class for one or more
child classes.
• The visibility mode for each derived class is specified separately during the inheritance and it
accesses the data members accordingly.
10/24/2023
PRACTICE PROBLEM
Class Vehicle:
•Vehicle Class will have a constructor, which will say "This is a Vehicle".
Class Car:
•Car Class will derive the Vehicle Class and have a constructor. it will say "This is a Car".
Class Bike:
•Bike Class will derive the Vehicle Class and have a constructor. it will say "This is a Bike".
•Now, Consider an Integer N, which will denote the number of tyres in the vehicles. You have to
create an object of the appropriate class according to the value of N.
•If N = 2, Create a Bike Object. If N = 4,
Create a Car Object.
•Create a Vehicle Object, otherwise. Input
•4
•Output
•This is a Vehicle This is a Car
PRACTICE PROBLEM
class Vehicle { class Bike : public Vehicle { Bike obj;
cout << "This is a Vehicle "; cout << "This is a Bike "; } else {
Car() { int N; }
}}; if (N == 2) {
Lecture 8
HYBRID INHERITANCE
• Hybrid Inheritance, as the name suggests, is the combination of two or over two types of
inheritances. For example, the classes in a program are in such an arrangement that they show
both single inheritance and hierarchical inheritance at the same time. Such an arrangement is
known as the Hybrid Inheritance.
10/24/2023
HYBRID INHERITANCE
class Person { "Working..." << endl; }};
public: class Intern : public
void showIdentity() class Student : virtual Employee, public Student
{ cout << "I am a Person" public Person { {
<< endl; }}; public: public:
class Employee : virtual void doInternship()
void study() { cout <<
public Person { "Studying..." << endl; } { cout << "Doing
public: Internship..." << endl; }
};
void work() { cout << };