Intro To Oop

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

CMP 221: COMPUTER

PROGRAMMING II
TOPIC: OBJECT ORIENTED
PROGRAMMING
• What is Object-Oriented Programming?
• Object-oriented programming (OOP) is a fundamental
programming paradigm based on the concept
of “objects”.
• These objects can contain data in the form of fields (often
known as attributes or properties) and code in the form
of procedures (often known as methods).
• The core concept of the object-oriented approach is to
break complex problems into smaller objects.
• Object-oriented programming has several advantages over
procedural programming:
• OOP is faster and easier to execute
• OOP provides a clear structure for the programs
• OOP helps to keep the Java code DRY "Don't Repeat Yourself",
and makes the code easier to maintain, modify and debug
• OOP makes it possible to create full reusable applications with
less code and shorter development time
• Tip: The "Don't Repeat Yourself" (DRY) principle is about
reducing the repetition of code.
• You should extract out the codes that are common for the
application, and place them at a single place and reuse them
instead of repeating it.
• There are some basic concepts that act as the building blocks of OOPs
i.e.
• Class
• Objects
• Encapsulation
• Abstraction
• Polymorphism
• Inheritance
• Dynamic Binding
• Message Passing
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.
• A class is like a blueprint for an object.
• For Example: Consider the Class of Cars. There may be many cars
with different names and brands but all of them will share some
common properties like all of them will have 4 wheels, Speed Limit,
Mileage range, etc.
• So here, the Car is the class, and wheels, speed limits, and mileage are
their properties.
Object
• An Object is an identifiable entity with some characteristics and
behavior. 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.
#include <iostream>
using namespace std; We have used the class keyword to create a
class car {
class named Car. Here,
char brand[20]; brand and model are class attributes used to
char model[20]; store data
int id;
drive() is a class function used to perform some
public: operation
void drive() {}
};

int main()
{

person p1; // p1 is a object


return 0;
}
• Difference Between Structure and Class in C++
Class Structure

1. Members of a class are private by default. 1. Members of a structure are public by default.

2. An instance of structure is called the ‘structure


2. An instance of a class is called an ‘object’.
variable’.

3. Member classes/structures of a class are private


3. Member classes/structures of a structure are
by default but not all programming languages
public by default.
have this default behavior eg Java etc.

4. It is declared using the class keyword. 4. It is declared using the struct keyword.

5. It is normally used for data abstraction and


5. It is normally used for the grouping of data
further inheritance.

6. NULL values are possible in Class. 6. NULL values are not possible.

7. Syntax: 7. Syntax:
class class_name{ struct structure_name{
data_member; type structure_member1;
member_function; type structure_member2;
}; };
• C++ Encapsulation
• In C++, object-oriented programming allows us to bundle together
data members (such as variables, arrays, etc.) and its related functions
into a single entity. This programming feature is known as
encapsulation.
• In the example below, we bundled together the related variables
brand, model and mileage with the function show_data() into a class
named Car.
class Car {
public:

// class data
string brand;
string model;
int mileage = 0;

// class function
void show_data() {
// code
}
};
• C++ Abstraction
• In object-oriented programming, abstraction refers to the concept of
showing only the necessary information to the user i.e. hiding the
complex details of program implementation and execution. For
example, let us consider a slightly modified version of the Car class:

class Car {
private:

// class data
int speed;

// class function
void show_car_status() {
// code
}
};
• Suppose we want the show_car_status() function to show the status of
the car based on the value of the speed variable.
• We can implement such conditional statements using the if...else
statement inside the show_car_status() function.
void show_car_status() {
if (speed != 0)
cout << "The car is being driven." << endl;
else
cout << "The car is stationary." << endl;
}

• Here, we do not need to show the user all the codes we have written
to determine if the car is stationary or not; we just need to show them
whether the car is being driven or not depending on its speed.
• In other words, we are only giving useful and relevant information to
the user, while hiding all the unnecessary details.
• This is data abstraction in OOP.
• C++ Inheritance
• Inheritance in C++ allows us to create a new class (derived class)
from an existing class (base class).
• The derived class inherits features from the base class and can have
additional features of its own.
#include <iostream>
using namespace std;

// base class
int main() {
class Vehicle {
public:
// create an object of Car class
Car my_car;
string brand;
// initialize variables of my_car
void show_brand() {
my_car.brand = "Honda";
cout << "Brand: " << brand << endl;
my_car.model = "Accord";
}
};
// display variables of my_car
my_car.show_brand();
// derived class
my_car.show_model();
class Car : public Vehicle {
public:
return 0;
}
string model;

void show_model() {
cout << "Model: " << model << endl;
}
};
• Here,
• Vehicle is the base class.
• Car is the derived class.
• The derived class inherits the features of the base class.
• We can see this from the brand variable and the show_brand()
function, since the Car object my_car can access them.
• In addition to the features of the base class, the derived class also
has features of its own. The unique features of the Car class are:
• model - a string variable
• show_model() - a function that prints the model variable.
• We can also see that the Vehicle class has not been modified by
its derived class.
• C++ Polymorphism
• Polymorphism is the ability to use a common function (or
operator) in multiple ways.
• In C++, polymorphism is implemented with the help of
function overloading, operator overloading, function overriding,
and virtual functions.
• Let's look at function overriding as an example.
#include <iostream>
using namespace std;

// base class
class Shape {
public:
int main() {
// function of base class
void shape_name() { // create class objects
cout << "Shape" << endl; Shape shape;
} Square square;
};
// call function from Shape class
// derived class shape.shape_name();
class Square : public Shape {
public: // call function from Square class
square.shape_name();
// overriding function of derived class
void shape_name() { return 0;
cout << "Square" << endl; }
}
};
• Here,
• Shape is the base class.
• Square is the derived class.
• Both these classes have a function called shape_name(). But the
body of the shape_name() function are different in the two
classes.
• When the shape_name() function is called by the shape object,
the code inside the function of the Shape class is executed.
• However, when shape_name() is called by the square object, the
code inside the body of Square class is executed.
• Thus, we have used the same function shape_name() in two
different ways using C++ polymorphism.
• Message Passing
• Objects communicate with one another by sending and receiving
information.
• A message for an object is a request for the execution of a procedure
and therefore will invoke a function in the receiving object that
generates the desired results.
• Message passing involves specifying the name of the object, the name
of the function, and the information to be sent.
#include <iostream>
using namespace std;

// Define a Car class with a method to display its speed


class Car {
public:
void displaySpeed(int speed) {
cout << "The car is moving at " << speed << " km/h." << endl;
}
};

int main() {
// Create a Car object named myCar
Car myCar;

// Send a message to myCar to execute the displaySpeed method


int currentSpeed = 100;
myCar.displaySpeed(currentSpeed);

return 0;
}
• C++ Access Modifiers
• One of the main features of object-oriented programming languages
such as C++ is data hiding.
• Data hiding refers to restricting access to data members of a class. This
is to prevent other functions and classes from tampering with the class
data.
• However, it is also important to make some member functions and
member data accessible so that the hidden data can be manipulated
indirectly.
• The access modifiers of C++ allows us to determine which class
members are accessible to other classes and functions, and which are
not.
class Patient {

private:
int patientNumber;
string diagnosis;

public:

void billing() {
// code
}

void makeAppointment() {
// code
}
};

Here, the variables patientNumber and diagnosis of the Patient class are
hidden using the private keyword, while the member functions are made
accessible using the public keyword.
• Types of C++ Access Modifiers
• In C++, there are 3 access modifiers:
• public
• private
• Protected

• public Access Modifier


• The public keyword is used to create public members (data and
functions).
• The public members are accessible from any part of the program.
• private Access Modifier
• The private keyword is used to create private members (data and
functions).
• The private members can only be accessed from within the class.
• However, friend classes and friend functions can access private
members.

• protected Access Modifier


• The protected keyword is used to create protected members (data
and function).
• The protected members can be accessed within the class and from the
derived class.

• Note: By default, class members in C++ are private, unless specified


otherwise.

You might also like