Project Oops New
Project Oops New
1
EXPERIMENT NO. 01
#include <iostream>
class Animal {
public:
string name;
Animal(string n) {
name = n;
void speak() {
};
int main() {
Animal dog("Dog");
return 0;}
2
Output
3
EXPERIMENT NO. 02
#include <iostream>
class BankAccount {
private:
double balance;
public:
BankAccount(double bal) {
balance = bal;
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
#include <iostream>
class Vehicle {
public:
void startEngine() {
};
public:
void drive() {
};
int main() {
Car car;
7
Output
Engine started.
Car is driving.
8
EXPERIMENT NO. 04
#include <iostream>
class Animal {
public:
};
public:
};
9
public:
};
int main() {
Animal* animal;
Bird bird;
Dog dog;
animal = &bird;
animal = &dog;
return 0;
10
Output
Chirp
Bark
11
EXPERIMENT NO. 05
#include <iostream>
class Shape {
public:
};
private:
double radius;
public:
Circle(double r) : radius(r) {}
};
int main() {
12
Circle circle(5);
cout << "Area of circle: " << circle.area() << endl; // Output: 78.5
return 0;
Output
13
EXPERIMENT NO. 06
#include <iostream>
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
#include <iostream>
class MathOperations {
public:
return a + 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
17
EXPERIMENT NO. 08
#include <iostream>
class StaticBinding {
public:
};
class DynamicBinding {
public:
};
int main() {
return 0;
Output
19
EXPERIMENT NO. 09
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
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.
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