oops practical
oops practical
#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>
class Number {
int a, b;
public:
void input() {
};
void add(Number n) {
int main() {
Number obj;
obj.input();
add(obj);
return 0;
}
Sample Output:
5. Write a program for Inline Function
#include <iostream>
return x * x;
return x * x * x;
int main() {
int num;
return 0;
}
Sample Output:
6. Write a program for Function Overloading
#include <iostream>
void display(int a) {
void display(double b) {
void display(string s) {
int main() {
int x;
double y;
string z;
cin >> x;
cin >> y;
cin >> z;
display(x);
display(y);
display(z);
return 0;
Sample Output:
7. Write a program for Nesting of Member Functions
#include <iostream>
class Number {
int a, b;
int sum() {
return a + b;
public:
void getData() {
void showSum() {
};
int main() {
Number n;
n.getData();
n.showSum();
return 0;
}
Sample Output:
8. Write a program for Parameterized Constructor
#include <iostream>
class Rectangle {
public:
Rectangle(int l, int b) {
length = l;
breadth = b;
void area() {
};
int main() {
int 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;
name = s.name;
age = s.age;
void display() {
} };
int main() {
string n;
int a;
cout << "Enter name: ";
cin >> n;
cin >> a;
Student s2 = s1;
s2.display();
return 0;
}
Sample Output:
10. Write a program for Constructor Overloading
#include <iostream>
class Box {
private:
double length;
double breadth;
double height;
public:
Box() {
length = 0;
breadth = 0;
height = 0;
Box(double len) {
length = len;
breadth = 0;
height = 0;
length = len;
breadth = bre;
height = hei;
double getVolume() {
};
int main() {
// Taking user input for the first box (no arguments constructor)
Box box1;
cout << "Enter the length, breadth, and height for Box 3: ";
return 0;
}
Sample Output:
11. Write a program for Destructor
#include <iostream>
class Box {
private:
double length;
double breadth;
double height;
public:
Box() {
length = 0;
breadth = 0;
height = 0;
Box(double len) {
length = len;
breadth = 0;
height = 0;
length = len;
breadth = bre;
height = hei;
// Destructor
~Box() {
double getVolume() {
};
int main() {
// Taking user input for the first box (no arguments constructor)
Box box1;
Box box2(len);
cout << "Enter the length, breadth, and height for Box 3: ";
return 0;
}
Sample Output:
12. Write a program for Virtual Function
#include <iostream>
// Base class
class Shape {
public:
return 0;
};
private:
double length;
double breadth;
double height;
public:
Box() {
length = 0;
breadth = 0;
height = 0;
Box(double len) {
length = len;
breadth = 0;
height = 0;
cout << "Constructor with one argument called for Box!" << endl;
length = len;
breadth = bre;
height = hei;
cout << "Constructor with three arguments called for Box!" << endl;
}
};
int main() {
// Taking user input for the first box (no arguments constructor)
Box box1;
Box box2(len);
cout << "Enter the length, breadth, and height for Box 3: ";
Shape* shapePtr;
shapePtr = &box1;
shapePtr = &box2;
shapePtr = &box3;
return 0;
}
Sample Output:
13. Write a program for This Pointer
#include <iostream>
class Box {
private:
double length;
public:
Box(double length) {
this->length = length;
this->length = length;
return *this;
}
bool equals(const Box& other) {
};
int main() {
Box box1(length1);
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>
class Animal {
protected:
string name;
public:
name = animalName;
void eat() {
};
private:
string breed;
public:
breed = dogBreed;
void bark() {
void displayDetails() {
};
int main() {
getline(cin, dogName);
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>
class Animal {
protected:
string name;
public:
name = animalName;
void eat() {
};
protected:
string breed;
public:
breed = dogBreed;
void bark() {
};
private:
string color;
public:
color = puppyColor;
void displayDetails() {
};
int main() {
getline(cin, puppyName);
getline(cin, puppyBreed);
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>
class Camera {
protected:
string cameraType;
public:
cameraType = type;
void takePhoto() {
cout << "Taking photo with " << cameraType << " camera..." << endl;
};
class Phone {
protected:
string brand;
public:
brand = phoneBrand;
void makeCall() {
cout << "Making a call from " << brand << " phone..." << endl;
};
public:
void displayDetails() {
};
int main() {
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>
class Camera {
protected:
string cameraType;
public:
cameraType = type;
void takePhoto() {
cout << "Taking photo with " << cameraType << " camera..." << endl;
};
class Phone {
protected:
string brand;
public:
brand = phoneBrand;
void makeCall() {
cout << "Making a call from " << brand << " phone..." << endl;
};
protected:
string operatingSystem;
public:
operatingSystem = os;
void displayDetails() {
};
private:
string features;
public:
features = feature;
void displayAdvancedDetails() {
displayDetails();
};
int main() {
getline(cin, cameraType);
getline(cin, operatingSystem);
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>
class Vehicle {
protected:
string vehicleType;
public:
vehicleType = type;
void displayVehicleType() {
};
protected:
string engineType;
public:
engineType = type;
void displayEngineType() {
};
protected:
string bodyType;
public:
bodyType = type;
void displayBodyType() {
}
};
private:
string carBrand;
public:
carBrand = brand;
void displayCarDetails() {
displayVehicleType();
displayEngineType();
displayBodyType();
};
int main() {
getline(cin, brand);
getline(cin, engineType);
getline(cin, bodyType);
car.displayCarDetails();
return 0;
}
Sample Output:
19. Write a program for Compile-time Polymorphism
#include <iostream>
class Calculator {
public:
return a + b;
return a + b;
class Complex {
public:
int real;
int imag;
return temp;
void display() {
cout << "Real: " << real << " Imag: " << imag << endl;
};
};
int main() {
Calculator calc;
cout << "Enter real and imaginary parts of the first complex number: ";
cout << "Enter real and imaginary parts of the second complex number: ";
c3 = c1 + c2;
c3.display();
return 0;
}
Sample Output:
20. Write a program for Run-time Polymorphism
#include <iostream>
class Animal {
public:
};
public:
};
public:
};
int main() {
int choice;
Animal* animal;
if (choice == 1) {
Dog dog;
animal = &dog;
} else if (choice == 2) {
Cat cat;
animal = &cat;
} else {
return 0;
animal->sound();
return 0;
}
Sample Output: