You are on page 1of 4

Assignment 1 oops SE 202

Name :Mohit
Roll no.= 2K22/A2/115

Difference between Structured Programming and Object-Oriented


Programming :
Structured Programming Object-Oriented Programming

It is a subset of procedural It relies on concept of objects that contain


programming. data and code.

Programs are divided into small Programs are divided into objects or
programs or functions. entities.

It is all about facilitating creation of


It is all about creating objects that usually
programs with readable code and
contain both functions and data.
reusable components.

Its main aim is to improve and increase Its main aim is to improve and increase both
quality, clarity, and development time quality and productivity of system analysis
of computer program. and design.

It simply focuses on representing both


It simply focuses on functions and structure and behavior of information
processes that usually work on data. system into tiny or small modules that
generally combines data and process both.

It is a method of organizing, managing


It is a method in which set of objects can
and coding programs that can give or
vary dynamically and can execute just by
provide much easier modification and
acting and reading to each other.
understanding.

In this, methods are written globally


In this, method works dynamically, make
and code lines are processed one by
calls as per need of code for certain time.
one i.e., Run sequentially.

It generally follows “Top-Down It generally follows “Bottom-Up


Approach”. Approach”.

It provides less flexibility and


It provides more flexibility and abstraction
abstraction as compared to object-
as compared to structured programming.
oriented programming.

It is more difficult to modify structured It is less difficult to modify object-oriented


program and reuse code as compared to programs and reuse code as compared to
object-oriented programs. structured programs.

It gives more importance of code. It gives more importance to data.

Q2. Take any real world entity and write a code snippet for identification of class, object,
encapsulation, abstraction, polymorphism, inheritance

using the example of a "Car" in an object-oriented context, followed by a code


snippet in C++ that demonstrates these concepts:

Class: In C++, a class is a blueprint for creating objects. It defines the


attributes (data members) and methods (functions) that objects of that
class will have. For example, a "Car" class would define attributes like make,
model, and year, as well as methods like start and stop.

Object: An object is an instance of a class. When you create an object, you


are essentially creating a concrete representation of the class with its
specific attribute values. For instance, a "Toyota Camry 2022" could be an
object of the "Car" class.

Encapsulation: Encapsulation involves bundling data (attributes) and


methods (functions) that operate on that data within a single unit (the
class). This concept promotes data hiding and controlled access to the
object's internals. In our "Car" class, attributes like make, model, and year,
and methods like start and stop, would be encapsulated.

Abstraction: Abstraction hides complex implementation details and


exposes only the essential features to the user. This simplifies the
interaction with the object. For example, a user doesn't need to know the
intricate details of how the car's engine works to use methods like start and
stop.
Polymorphism: Polymorphism allows objects of different classes to be
treated as objects of a common superclass through inheritance. This
enables you to write code that can work with different types of objects in a
uniform way. In our "Car" example, polymorphism can be demonstrated by
having different types of cars share a common "start" method.

Inheritance: Inheritance is a mechanism in which one class (the derived or


child class) inherits the attributes and methods of another class (the base or
parent class). This promotes code reuse and allows the child class to extend
or override the behavior of the parent class. For instance, an "ElectricCar"
class could inherit from the "Car" class and add its own attributes and
methods.

Now, here's a code snippet in C++ that demonstrates these concepts:

#include <iostream>
using namespace std;

// Class definition
class Car {
public:
string make;
string model;
int year;
bool isRunning;

Car(string mk, string mdl, int yr) : make(mk), model(mdl), year(yr),


isRunning(false) {}

void start() {
isRunning = true;
cout << year << " " << make << " " << model << " is now running." << endl;
}

void stop() {
isRunning = false;
cout << year << " " << make << " " << model << " has stopped." << endl;
}
};
// Subclass ElectricCar inheriting from Car
class ElectricCar : public Car {
public:
ElectricCar(string mk, string mdl, int yr) : Car(mk, mdl, yr) {}

// Polymorphism: Override the start method


void start() {
isRunning = true;
cout << year << " " << make << " " << model << " (Electric) is now running
silently." << endl;
}
};

int main() {
// Creating objects
Car car1("Toyota", "Camry", 2022);
ElectricCar electricCar1("Tesla", "Model S", 2023);

// Encapsulation and Abstraction


car1.start();
car1.stop();

electricCar1.start();
electricCar1.stop();

// Polymorphism
Car* cars[] = {&car1, &electricCar1};
for (Car* car : cars) {
car->start();
}

return 0;
}.

You might also like