Eeb 334-315 C++ Lecture 8 Ebenezer 2
Eeb 334-315 C++ Lecture 8 Ebenezer 2
By
DR. Ebenezer Esenogho
• Object Oriented Programming (OOP)
– Object-Oriented Thinking,
– Introduction to Classes,
– Objects
– Inheritance
– Polymorphism
• File Processing
Object-Oriented Thinking
• In the object-oriented thinking, we see the world as a set of
objects, and every action is a function of one of those objects.
The functional approach sees the world as a set of possible
actions, and objects only hold information.
• Class
• Objects
• Encapsulation
• Abstraction
• Polymorphism
• Inheritance
• Dynamic Binding
• Message Passing
• Let‘s dive deep into these building blocks to grasp the concept of OOP .
The structure, or building blocks, of object-oriented programming include
the following:
• Classes are user-defined data types that act as the blueprint for individual
objects, attributes and methods.
– Methods are functions that are defined inside a class that describe the
behaviours of an object. Each method contained in class definitions starts
with a reference to an instance object. Additionally, the subroutines
contained in an object are called instance methods. Programmers use
methods for reusability or keeping functionality encapsulated inside one
object at a time.
– Attributes are defined in the class template and represent the state of an
object. Objects will have data stored in the attributes field. Class attributes
belong to the class itself.
Class:
The building block of C++ that leads to Object-Oriented
programming is a Class. 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 (Object). A class is like
a blueprint/template for an object.
public:
// Constructor to initialize the book with a title and author
Book(std::string t, std::string a) : title(t), author(a) {}
public:
// Constructor to initialize the car with its attributes
Car(std::string b, std::string m, int y, std::string c)
: brand(b), model(m), year(y), color(c) {}
• Declaring Objects
• When a class is defined, only the specification for the object is
defined; no memory or storage is allocated. To use the data and
access functions defined in the class, you need to create objects.
• Syntax
• ClassName ObjectName;
#include <iostream>
#include <string>
using namespace std;
int main() {
MyClass myObj; // Create an object of MyClass
// Print values
cout << myObj.myNum << "\n";
cout << myObj.myString;
return 0;
}
// create an object myCircle. The area of this circle is then computed and displayed using its area method.
#include <iostream>
public:
// Constructor to initialize the radius of the circle
Circle(double r) : radius(r) {}
#include <iostream>
#include <string>
using namespace std;
class Car {
public:
string brand;
string model;
int year;
};
int main() {
Car carObj1;
carObj1.brand = "BMW";
carObj1.model = "X5";
carObj1.year = 1999;
Car carObj2;
carObj2.brand = "Ford";
carObj2.model = "Mustang";
carObj2.year = 1969;
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0;
}
#include <iostream>
#include <string>
using namespace std;
class Car {
public:
Car(string b, string m, int y) : brand(b), model(m), year(y) {}
void display() {
cout << brand << " " << model << " " << year << "\n";
}
private:
string brand;
string model;
int year;
};
int main() {
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);
carObj1.display();
carObj2.display();
return 0;
Assignment 1.
The person who needs to check his account balance will only be
able to access private members via methods defined inside that
class, and this method will demand your account holder's name
and password for authentication.
As a result, we can achieve security by using the principle of data
hiding. This is known as encapsulation.
• So, the meaning of Encapsulation, is to make sure that "sensitive"
data is hidden from users. To achieve this, you must declare class
variables/attributes as private (cannot be accessed from outside
the class). If you want others to read or modify the value of a
private member, you can provide public get and set methods.
• In this code, the Person class encapsulates the name and age
attributes. External access to these attributes is controlled using
getter and setter methods.
#include <iostream>
#include <string>
public:
// Constructor to initialize the person's attributes
Person(std::string n, int a) : name(n), age(a) {}
// Getter method for the name (allows reading the private attribute 'name')
std::string getName() const {
return name;
}
// Setter method for the name (allows modifying the private attribute 'name')
void setName(const std::string &n) {
name = n;
}
return 0;
#include <iostream>
using namespace std;
class Employee {
private:
int salary;
public:
void setSalary(int s) {
salary = s;
}
int getSalary() {
return salary;
}
};
int main() {
Employee myObj;
myObj.setSalary(10000);
cout << myObj.getSalary();
return 0;
}
The salary attribute is private, which have restricted access.
The public getSalary() method returns the value of the private salary
attribute.
• When we say derived class inherits the base class, it means, the
derived class inherits all the properties of the base class, without
changing the properties of base class and may add new features
to its own. These new features in the derived class will not affect
the base class. The derived class is the specialized class for the
base class.
• Sub Class: The class that inherits properties from another class is
called Subclass or Derived Class or Child Class.
• Super Class: The class whose properties are inherited by a
subclass is called Base Class or Superclass or Parent Class.
• Why and when to use inheritance?
• Consider a group of vehicles. You need to create classes for Bus,
Car, and Truck. The methods fuelAmount(), capacity(),
applyBrakes() will be the same for all three classes. If we create
these classes avoiding inheritance then we have to write all of
these functions in each of the three classes as shown below figure:
You can clearly see that the above process results in duplication of
the same code 3 times. This increases the chances of error and data
redundancy. To avoid this type of situation, inheritance is used. If we
create a class Vehicle and write these three functions in it and inherit
the rest of the classes from the vehicle class, then we can simply
avoid the duplication of data and increase re-usability. Look at the
below diagram in which the three classes are inherited from vehicle
class:
Common
characteristics
Using inheritance, we have to write the functions only one time instead
of three times as we have inherited the rest of the three classes from the
base class (Vehicle).
Syntax:
Example 1. the Car class (child) inherits the attributes and methods from the Vehicle
class (parent)
int main() {
// Creating an object of the Car class
Car myCar;
// Using the honk method from the Vehicle class (inherited by Car)
myCar.honk();
// Displaying the brand (from Vehicle) and model (from Car) of myCar
cout << myCar.brand + " " + myCar.model;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
Student stu("Alice", 20, "Computer Science");
stu.display();
return 0;
}
Types Of Inheritance:-
• Single inheritance
• Multilevel inheritance
• Multiple inheritance
• Hierarchical inheritance
• Hybrid inheritance.
Polymorphism:
• In the C++ OOPs concept, polymorphism means taking more than
one form. For example, imagine you are in a classroom at that
time. You behave like a student. When you are in the market at
that time, you behave like a customer/trader. When you are at
your home at that time, you behave like a son or daughter. In this
case, one person is exhibiting a variety of behaviours.
int main() {
string a = "poly";
string b = "morphism";
cout << "Value of a + b is: " << a + b;
return 0;
}
//output is Value of a + b is: polymorphism
Thank you
Kea leboga
Abstraction