0% found this document useful (0 votes)
7 views

Project Oops New

The document outlines core concepts of object-oriented programming (OOP) including classes, encapsulation, inheritance, polymorphism, abstraction, and object lifecycle management. It provides example programs demonstrating these concepts through practical coding exercises. Additionally, it discusses the Object Model in OOP, detailing how objects, classes, encapsulation, inheritance, polymorphism, abstraction, identity, and method invocation interact within a software system.

Uploaded by

s8305792
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Project Oops New

The document outlines core concepts of object-oriented programming (OOP) including classes, encapsulation, inheritance, polymorphism, abstraction, and object lifecycle management. It provides example programs demonstrating these concepts through practical coding exercises. Additionally, it discusses the Object Model in OOP, detailing how objects, classes, encapsulation, inheritance, polymorphism, abstraction, identity, and method invocation interact within a software system.

Uploaded by

s8305792
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

EXPERIMENT-TOPIC

 Classes and Objects: Core Concepts

 Encapsulation: Hiding and Protecting Data

 Inheritance: Reusing and Extending Code

 Polymorphism: Achieving Flexibility in Behavior

 Abstraction: Simplifying Complex Systems

 Constructors and Destructors: Object Lifecycle Management

 Overloading and Overriding: Methods and Operators in OOP

 Static vs. Dynamic Binding in OOP

 Study Object Model

1
EXPERIMENT NO. 01

 Program for Classes and Objects: Core Concepts

#include <iostream>

using namespace std;

class Animal {

public:

string name;

Animal(string n) {

name = n;

void speak() {

cout << name << " makes a sound." << endl;

};

int main() {

Animal dog("Dog");

dog.speak(); // Output: Dog makes a sound.

return 0;}

2
Output

Dog makes a sound.

3
EXPERIMENT NO. 02

 Program for Encapsulation: Hiding and Protecting Data

#include <iostream>

using namespace std;

class BankAccount {

private:

double balance;

public:

BankAccount(double bal) {

balance = bal;

void deposit(double amount) {

balance += amount;

double getBalance() {

return balance;

4
};

int main() {

BankAccount account(1000);

account.deposit(500);

cout << "Balance: " << account.getBalance() << endl; // Output: Balance: 1500

return 0;

5
Output

Balance: 1500

6
EXPERIMENT NO. 03

 Program for Inheritance: Reusing and Extending Code

#include <iostream>

using namespace std;

class Vehicle {

public:

void startEngine() {

cout << "Engine started." << endl;

};

class Car : public Vehicle {

public:

void drive() {

cout << "Car is driving." << endl;

};

int main() {

Car car;

car.startEngine(); // Output: Engine started.


car.drive(); // Output: Car is driving.
return 0;
}

7
Output

Engine started.

Car is driving.

8
EXPERIMENT NO. 04

 Program for Polymorphism: Achieving Flexibility in Behavior

#include <iostream>

using namespace std;

class Animal {

public:

virtual void sound() {

cout << "Some animal sound" << endl;

};

class Bird : public Animal {

public:

void sound() override {

cout << "Chirp" << endl;

};

class Dog : public Animal {

9
public:

void sound() override {

cout << "Bark" << endl;

};

int main() {

Animal* animal;

Bird bird;

Dog dog;

animal = &bird;

animal->sound(); // Output: Chirp

animal = &dog;

animal->sound(); // Output: Bark

return 0;

10
Output
Chirp

Bark

11
EXPERIMENT NO. 05

 Program for Abstraction: Simplifying Complex Systems

#include <iostream>

using namespace std;

class Shape {

public:

virtual double area() = 0; // Pure virtual function

};

class Circle : public Shape {

private:

double radius;

public:

Circle(double r) : radius(r) {}

double area() override {

return 3.14 * radius * radius;

};

int main() {

12
Circle circle(5);

cout << "Area of circle: " << circle.area() << endl; // Output: 78.5

return 0;

Output

Area of circle: 78.5

13
EXPERIMENT NO. 06

 Program for Constructors and Destructors: Object Lifecycle Management

#include <iostream>

using namespace std;

class Person {

public:

string name;

Person(string n) {

name = n;

cout << "Hello, " << name << "!" << endl;

~Person() {

cout << "Goodbye, " << name << "!" << endl;

};

int main() {

Person p("Alice");

return 0;

14
Output

Hello, Alice!

Goodbye, Alice!

15
EXPERIMENT NO. 07

 Program for Overloading and Overriding: Methods and Operators in OOP

#include <iostream>

using namespace std;

class MathOperations {

public:

int add(int a, int b) {

return a + b;

double add(double a, double b) {

return a + b;

};

int main() {

MathOperations math;

cout << "Sum of integers: " << math.add(1, 2) << endl; // Output: 3

cout << "Sum of doubles: " << math.add(1.1, 2.2) << endl; // Output: 3.3

return 0;

16
Output

Sum of integers: 3

Sum of doubles: 3.3

17
EXPERIMENT NO. 08

 Program for Static vs. Dynamic Binding in OOP

#include <iostream>

using namespace std;

class StaticBinding {

public:

static void greet() {

cout << "Hello, this is static binding!" << endl;

};

class DynamicBinding {

public:

virtual void greet() {

cout << "Hello, this is dynamic binding!" << endl;

};

int main() {

StaticBinding::greet(); // Output: Hello, this is static binding!

DynamicBinding* db = new DynamicBinding();

db->greet(); // Output: Hello, this is dynamic binding!


18
delete db;

return 0;

Output

Hello, this is static binding!

Hello, this is dynamic binding!

19
EXPERIMENT NO. 09

Study Object Model in brief :

The Object Model in object-oriented programming (OOP) is a conceptual framework that


defines how objects are created, structured, and interact within a system. It represents the way
data and behaviors are encapsulated in objects and how these objects relate to one another within
a software system. In simpler terms, it's a model that outlines how the real-world entities are
mapped into software objects, and how these objects interact with each other.

Here are the key components of the Object Model:

1. Objects

 Objects are instances of classes. They represent real-world entities or concepts in the software
world. Each object has its own identity, state (attributes), and behavior (methods).
 For example, a Car class could have objects like myCar or yourCar.

2. Classes

 A class is a blueprint or template from which objects are created. It defines the properties
(attributes) and behaviors (methods) that the objects of that class will have.
 A class defines the type of object, but the actual object is created from the class and has its own
set of values for the attributes.

3. Encapsulation

 Encapsulation is the concept of bundling the data (attributes) and the methods that operate on
the data into a single unit (i.e., a class), and restricting access to the internal state of an object.
This ensures that an object’s internal state is hidden from the outside world and can only be
accessed through well-defined methods (getters and setters).

4. Inheritance

 Inheritance allows a class (child class) to inherit properties and behaviors (methods) from
another class (parent class). This promotes reusability and the creation of hierarchical
relationships between classes.
 For example, a Car class could inherit from a Vehicle class, allowing it to share common
attributes and methods while also having its own specific features.

20
5. Polymorphism

 Polymorphism allows objects of different classes to be treated as objects of a common


superclass. It enables one interface to be used for a general class of actions. The specific action
is determined at runtime (dynamic polymorphism) or compile-time (static polymorphism).
 For instance, different types of Vehicle objects (Car, Truck, etc.) can be treated as Vehicle
objects but each can call their own specific startEngine method.

6. Abstraction

 Abstraction hides the complex reality while exposing only the necessary parts. It allows you to
focus on high-level mechanisms without dealing with the details of their implementation.
 For example, when driving a car, you interact with a simplified interface (the steering wheel,
pedals, etc.), but you don't need to know the inner workings of the engine to drive.

7. Identity

 Every object has a unique identity, even if two objects have the same attributes. This identity
distinguishes each object from others.

8. Messages/Method Invocation

 In the object model, messages are sent to objects to invoke their behaviors (methods). Objects
communicate with each other by calling methods on each other.

Example of an Object Model:

Let’s consider a simple object model of a School:

 Class: Person
o Attributes: name, age
o Methods: greet()
o
 Class: Teacher (inherits from Person)
o Attributes: subject
o Methods: teach()
o
 Class: Student (inherits from Person)
o Attributes: grade
o Methods: study()

21
Diagrammatically:
markdown
Copy code
Person
/ \
Teacher Student

 Teacher and Student both inherit from Person, and each has its own specific behavior like
teach() or study().
 Both the Teacher and Student objects have common attributes like name and age inherited
from the Person class, but they also have their unique attributes (subject for Teacher,
grade for Student).

22

You might also like