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

oops practical

The document contains multiple C++ programs demonstrating various object-oriented programming concepts such as class implementation, static data members, friend functions, inline functions, function overloading, constructors, destructors, inheritance (single, multilevel, multiple, and hybrid), and the use of the 'this' pointer. Each program includes a brief description of its functionality and sample output. The programs serve as practical examples for understanding fundamental OOP principles in C++.

Uploaded by

itsharshkumar775
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

oops practical

The document contains multiple C++ programs demonstrating various object-oriented programming concepts such as class implementation, static data members, friend functions, inline functions, function overloading, constructors, destructors, inheritance (single, multilevel, multiple, and hybrid), and the use of the 'this' pointer. Each program includes a brief description of its functionality and sample output. The programs serve as practical examples for understanding fundamental OOP principles in C++.

Uploaded by

itsharshkumar775
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 68

1.

Write a program for Class Implementation

#include<iostream>
using namespace std;
class Student {
int id;
string name;
public:
void getData() {
cout << "Enter ID: ";
cin >> id;
cout << "Enter Name: ";
cin >> name;
}
void display() {
cout << "ID: " << id << ", Name: " << name << endl;
}
};
int main() {
Student s;
s.getData();
s.display();
return 0;
}
Sample Output:
2. Write a program for Static Data Members

#include<iostream>
using namespace std;
class Test {
static int count;
public:
void getData() {
count++;
}
void display() {
cout << "Count is: " << count << endl;
}
};
int Test::count = 0;
int main() {
Test t1, t2;
t1.getData();
t2.getData();
t1.display();
return 0;
}
Sample Output:
3. Write a program for Static Data Member Functions

#include<iostream>
using namespace std;
class Test {
static int value;
public:
static void setValue(int v) {
value = v;
}
static void display() {
cout << "Value is: " << value << endl;
}
};
int Test::value = 0;
int main() {
int v;
cout << "Enter value: ";
cin >> v;
Test::setValue(v);
Test::display();
return 0;
}
Sample Output:
4. Write a program for Friend Function
#include <iostream>

using namespace std;

class Number {

int a, b;

public:

void input() {

cout << "Enter two numbers: ";

cin >> a >> b;

friend void add(Number);

};

void add(Number n) {

int sum = n.a + n.b;

cout << "Sum = " << sum << endl;

int main() {

Number obj;

obj.input();

add(obj);

return 0;

}
Sample Output:
5. Write a program for Inline Function

#include <iostream>

using namespace std;

inline int square(int x) {

return x * x;

inline int cube(int x) {

return x * x * x;

int main() {

int num;

cout << "Enter a number: ";

cin >> num;

cout << "Square = " << square(num) << endl;

cout << "Cube = " << cube(num) << endl;

return 0;

}
Sample Output:
6. Write a program for Function Overloading
#include <iostream>

using namespace std;

void display(int a) {

cout << "Integer: " << a << endl;

void display(double b) {

cout << "Double: " << b << endl;

void display(string s) {

cout << "String: " << s << endl;

int main() {

int x;

double y;

string z;

cout << "Enter an integer: ";

cin >> x;

cout << "Enter a double: ";

cin >> y;

cout << "Enter a string: ";

cin >> z;
display(x);

display(y);

display(z);

return 0;

Sample Output:
7. Write a program for Nesting of Member Functions
#include <iostream>

using namespace std;

class Number {

int a, b;

int sum() {

return a + b;

public:

void getData() {

cout << "Enter two numbers: ";

cin >> a >> b;

void showSum() {

cout << "Sum = " << sum() << endl;

};

int main() {

Number n;

n.getData();
n.showSum();

return 0;

}
Sample Output:
8. Write a program for Parameterized Constructor
#include <iostream>

using namespace std;

class Rectangle {

int length, breadth;

public:

Rectangle(int l, int b) {

length = l;

breadth = b;

void area() {

cout << "Area = " << length * breadth << endl;

};

int main() {

int l, b;

cout << "Enter length and breadth: ";

cin >> l >> b;

Rectangle r(l, b);

r.area();

return 0;
}
Sample Output:
9. Write a program for Copy Constructor
#include <iostream>
using namespace std;

class Student {

string name;

int age;

public:

Student(string n, int a) {

name = n;

age = a;

Student(const Student &s) {

name = s.name;

age = s.age;

void display() {

cout << "Name: " << name << endl;

cout << "Age: " << age << endl;

} };

int main() {

string n;

int a;
cout << "Enter name: ";

cin >> n;

cout << "Enter age: ";

cin >> a;

Student s1(n, a);

Student s2 = s1;

s2.display();

return 0;

}
Sample Output:
10. Write a program for Constructor Overloading
#include <iostream>

using namespace std;

class Box {

private:

double length;

double breadth;

double height;

public:

// Constructor with no argument

Box() {

length = 0;

breadth = 0;

height = 0;

// Constructor with one argument

Box(double len) {

length = len;

breadth = 0;
height = 0;

// Constructor with three arguments

Box(double len, double bre, double hei) {

length = len;

breadth = bre;

height = hei;

// Function to calculate the volume

double getVolume() {

return length * breadth * height;

};

int main() {

double len, bre, hei;

// Taking user input for the first box (no arguments constructor)

Box box1;

cout << "Enter the length for Box 2: ";

cin >> len;


Box box2(len);

cout << "Enter the length, breadth, and height for Box 3: ";

cin >> len >> bre >> hei;

Box box3(len, bre, hei);

cout << "Volume of Box 1: " << box1.getVolume() << endl;

cout << "Volume of Box 2: " << box2.getVolume() << endl;

cout << "Volume of Box 3: " << box3.getVolume() << endl;

return 0;

}
Sample Output:
11. Write a program for Destructor
#include <iostream>

using namespace std;

class Box {

private:

double length;

double breadth;

double height;

public:

// Constructor with no argument

Box() {

length = 0;

breadth = 0;

height = 0;

cout << "Default constructor called!" << endl;

// Constructor with one argument

Box(double len) {

length = len;
breadth = 0;

height = 0;

cout << "Constructor with one argument called!" << endl;

// Constructor with three arguments

Box(double len, double bre, double hei) {

length = len;

breadth = bre;

height = hei;

cout << "Constructor with three arguments called!" << endl;

// Destructor

~Box() {

cout << "Destructor called! Object is being destroyed." << endl;

// Function to calculate the volume

double getVolume() {

return length * breadth * height;

};
int main() {

double len, bre, hei;

// Taking user input for the first box (no arguments constructor)

Box box1;

cout << "Enter the length for Box 2: ";

cin >> len;

Box box2(len);

cout << "Enter the length, breadth, and height for Box 3: ";

cin >> len >> bre >> hei;

Box box3(len, bre, hei);

cout << "Volume of Box 1: " << box1.getVolume() << endl;

cout << "Volume of Box 2: " << box2.getVolume() << endl;

cout << "Volume of Box 3: " << box3.getVolume() << endl;

return 0;

}
Sample Output:
12. Write a program for Virtual Function
#include <iostream>

using namespace std;

// Base class

class Shape {

public:

// Virtual function to calculate area

virtual double area() {

return 0;

// Virtual Destructor removed

};

// Derived class for Box

class Box : public Shape {

private:

double length;

double breadth;

double height;
public:

// Constructor with no argument

Box() {

length = 0;

breadth = 0;

height = 0;

cout << "Default constructor called for Box!" << endl;

// Constructor with one argument

Box(double len) {

length = len;

breadth = 0;

height = 0;

cout << "Constructor with one argument called for Box!" << endl;

// Constructor with three arguments

Box(double len, double bre, double hei) {

length = len;

breadth = bre;

height = hei;

cout << "Constructor with three arguments called for Box!" << endl;
}

// Override virtual function to calculate the volume

double area() override {

return length * breadth * height; // Volume calculation for Box

};

int main() {

double len, bre, hei;

// Taking user input for the first box (no arguments constructor)

Box box1;

cout << "Enter the length for Box 2: ";

cin >> len;

Box box2(len);

cout << "Enter the length, breadth, and height for Box 3: ";

cin >> len >> bre >> hei;

Box box3(len, bre, hei);

// Using virtual function to calculate the volume (area)

Shape* shapePtr;
shapePtr = &box1;

cout << "Volume of Box 1: " << shapePtr->area() << endl;

shapePtr = &box2;

cout << "Volume of Box 2: " << shapePtr->area() << endl;

shapePtr = &box3;

cout << "Volume of Box 3: " << shapePtr->area() << endl;

return 0;

}
Sample Output:
13. Write a program for This Pointer
#include <iostream>

using namespace std;

class Box {

private:

double length;

public:

Box(double length) {

this->length = length;

Box& setLength(double length) {

this->length = length;

return *this;

void display() const {

cout << "Length: " << this->length << endl;

}
bool equals(const Box& other) {

return this->length == other.length;

};

int main() {

double length1, length2;

cout << "Enter the length of box1: ";

cin >> length1;

Box box1(length1);

cout << "Enter the length of box2: ";

cin >> length2;

Box box2(length2);

box1.setLength(length1).display();

cout << "box1 equals box2: " << (box1.equals(box2) ? "Yes" : "No") << endl;

return 0;

}
Sample Output:
14. Write a program for Single Inheritance
#include <iostream>

#include <string>

using namespace std;

class Animal {

protected:

string name;

public:

void setName(const string& animalName) {

name = animalName;

void eat() {

cout << name << " is eating..." << endl;

};

class Dog : public Animal {

private:

string breed;
public:

void setBreed(const string& dogBreed) {

breed = dogBreed;

void bark() {

cout << name << " is barking!" << endl;

void displayDetails() {

cout << "Dog Name: " << name << endl;

cout << "Breed: " << breed << endl;

};

int main() {

string dogName, dogBreed;

cout << "Enter the dog's name: ";

getline(cin, dogName);

cout << "Enter the dog's breed: ";


getline(cin, dogBreed);

Dog dog;

dog.setName(dogName);

dog.setBreed(dogBreed);

dog.displayDetails();

dog.eat();

dog.bark();

return 0;

}
Sample Output:
15. Write a program for Multilevel Inheritance
#include <iostream>

#include <string>

using namespace std;

class Animal {

protected:

string name;

public:

void setName(const string& animalName) {

name = animalName;

void eat() {

cout << name << " is eating..." << endl;

};

class Dog : public Animal {

protected:

string breed;
public:

void setBreed(const string& dogBreed) {

breed = dogBreed;

void bark() {

cout << name << " is barking!" << endl;

};

class Puppy : public Dog {

private:

string color;

public:

void setColor(const string& puppyColor) {

color = puppyColor;

void displayDetails() {

cout << "Puppy Name: " << name << endl;

cout << "Breed: " << breed << endl;


cout << "Color: " << color << endl;

};

int main() {

string puppyName, puppyBreed, puppyColor;

cout << "Enter the puppy's name: ";

getline(cin, puppyName);

cout << "Enter the puppy's breed: ";

getline(cin, puppyBreed);

cout << "Enter the puppy's color: ";

getline(cin, puppyColor);

Puppy puppy;

puppy.setName(puppyName);

puppy.setBreed(puppyBreed);

puppy.setColor(puppyColor);

puppy.displayDetails();

puppy.eat();

puppy.bark();

return 0;
}
Sample Output:
16. Write a program for Multiple Inheritance

#include <iostream>

#include <string>

using namespace std;

class Camera {

protected:

string cameraType;

public:

void setCameraType(const string& type) {

cameraType = type;

void takePhoto() {

cout << "Taking photo with " << cameraType << " camera..." << endl;

};

class Phone {

protected:

string brand;
public:

void setBrand(const string& phoneBrand) {

brand = phoneBrand;

void makeCall() {

cout << "Making a call from " << brand << " phone..." << endl;

};

class SmartPhone : public Phone, public Camera {

public:

void displayDetails() {

cout << "Phone Brand: " << brand << endl;

cout << "Camera Type: " << cameraType << endl;

};

int main() {

string phoneBrand, cameraType;

cout << "Enter the phone brand: ";


getline(cin, phoneBrand);

cout << "Enter the camera type: ";

getline(cin, cameraType);

SmartPhone phone;

phone.setBrand(phoneBrand);

phone.setCameraType(cameraType);

phone.displayDetails();

phone.makeCall();

phone.takePhoto();

return 0;

}
Sample Output:
17. Write a program for Hybrid Inheritance
#include <iostream>

#include <string>

using namespace std;

class Camera {

protected:

string cameraType;

public:

void setCameraType(const string& type) {

cameraType = type;

void takePhoto() {

cout << "Taking photo with " << cameraType << " camera..." << endl;

};

class Phone {

protected:

string brand;
public:

void setBrand(const string& phoneBrand) {

brand = phoneBrand;

void makeCall() {

cout << "Making a call from " << brand << " phone..." << endl;

};

class SmartPhone : public Phone, public Camera {

protected:

string operatingSystem;

public:

void setOperatingSystem(const string& os) {

operatingSystem = os;

void displayDetails() {

cout << "Phone Brand: " << brand << endl;

cout << "Camera Type: " << cameraType << endl;


cout << "Operating System: " << operatingSystem << endl;

};

class AdvancedSmartPhone : public SmartPhone {

private:

string features;

public:

void setFeatures(const string& feature) {

features = feature;

void displayAdvancedDetails() {

displayDetails();

cout << "Advanced Features: " << features << endl;

};

int main() {

string phoneBrand, cameraType, operatingSystem, features;

cout << "Enter the phone brand: ";


getline(cin, phoneBrand);

cout << "Enter the camera type: ";

getline(cin, cameraType);

cout << "Enter the operating system: ";

getline(cin, operatingSystem);

cout << "Enter the advanced features: ";

getline(cin, features);

AdvancedSmartPhone advancedPhone;

advancedPhone.setBrand(phoneBrand);

advancedPhone.setCameraType(cameraType);

advancedPhone.setOperatingSystem(operatingSystem);

advancedPhone.setFeatures(features);

advancedPhone.displayAdvancedDetails();

advancedPhone.makeCall();

advancedPhone.takePhoto();

return 0;

}
Sample Output:
18. Write a program for Virtual Base Class
#include <iostream>

#include <string>

using namespace std;

class Vehicle {

protected:

string vehicleType;

public:

Vehicle(const string& type) {

vehicleType = type;

void displayVehicleType() {

cout << "Vehicle Type: " << vehicleType << endl;

};

class Engine : virtual public Vehicle {

protected:

string engineType;
public:

Engine(const string& type) : Vehicle("Vehicle with Engine") {

engineType = type;

void displayEngineType() {

cout << "Engine Type: " << engineType << endl;

};

class Body : virtual public Vehicle {

protected:

string bodyType;

public:

Body(const string& type) : Vehicle("Vehicle with Body") {

bodyType = type;

void displayBodyType() {

cout << "Body Type: " << bodyType << endl;

}
};

class Car : public Engine, public Body {

private:

string carBrand;

public:

Car(const string& brand, const string& engineType, const string& bodyType)

: Vehicle("Car"), Engine(engineType), Body(bodyType) {

carBrand = brand;

void displayCarDetails() {

cout << "Car Brand: " << carBrand << endl;

displayVehicleType();

displayEngineType();

displayBodyType();

};

int main() {

string brand, engineType, bodyType;


cout << "Enter the car brand: ";

getline(cin, brand);

cout << "Enter the engine type: ";

getline(cin, engineType);

cout << "Enter the body type: ";

getline(cin, bodyType);

Car car(brand, engineType, bodyType);

car.displayCarDetails();

return 0;

}
Sample Output:
19. Write a program for Compile-time Polymorphism
#include <iostream>

using namespace std;

class Calculator {

public:

int add(int a, int b) {

return a + b;

double add(double a, double b) {

return a + b;

class Complex {

public:

int real;

int imag;

Complex(int r = 0, int i = 0) : real(r), imag(i) {}

Complex operator+(const Complex& c) {


Complex temp;

temp.real = real + c.real;

temp.imag = imag + c.imag;

return temp;

void display() {

cout << "Real: " << real << " Imag: " << imag << endl;

};

};

int main() {

Calculator calc;

int int1, int2;

double double1, double2;

cout << "Enter two integers: ";

cin >> int1 >> int2;

cout << "Enter two doubles: ";

cin >> double1 >> double2;


int sumInt = calc.add(int1, int2);

double sumDouble = calc.add(double1, double2);

cout << "Sum of integers: " << sumInt << endl;

cout << "Sum of doubles: " << sumDouble << endl;

int real1, imag1, real2, imag2;

cout << "Enter real and imaginary parts of the first complex number: ";

cin >> real1 >> imag1;

cout << "Enter real and imaginary parts of the second complex number: ";

cin >> real2 >> imag2;

Calculator::Complex c1(real1, imag1), c2(real2, imag2), c3;

c3 = c1 + c2;

cout << "Sum of complex numbers: ";

c3.display();

return 0;

}
Sample Output:
20. Write a program for Run-time Polymorphism
#include <iostream>

using namespace std;

class Animal {

public:

virtual void sound() {

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

};

class Dog : public Animal {

public:

void sound() override {

cout << "Bark" << endl;

};

class Cat : public Animal {

public:

void sound() override {

cout << "Meow" << endl;


}

};

int main() {

int choice;

Animal* animal;

cout << "Enter 1 for Dog or 2 for Cat: ";

cin >> choice;

if (choice == 1) {

Dog dog;

animal = &dog;

} else if (choice == 2) {

Cat cat;

animal = &cat;

} else {

cout << "Invalid choice!" << endl;

return 0;

animal->sound();

return 0;
}

Sample Output:

You might also like