Oops Experiment
Oops Experiment
#include<iostream>
using namespace std;
int main() {
int x, s = 0;
cout << "Enter the number : ";
cin >> x;
while (x != 0) {
s = s + x % 10;
x = x / 10;
}
cout << "\nThe sum of the digits : "<< s;
}
Output
Enter the number : 236214828
The sum of the digits : 36
Write a programme to create a class of a bank account with data members a/c no.
customer name and balance and member function withdraw amount and deposit
amount.
#include<iostream>
#include<string>
using namespace std;
class BankAccount {
private:
int accountNumber;
string customerName;
double balance;
public:
BankAccount(int accNum, string name, double initialBalance) {
accountNumber = accNum;
customerName = name;
balance = initialBalance;
}
void deposit(double amount) {
balance += amount;
cout << “Deposit of “ << amount << “ made. New balance is “ << balance << “.” << endl;
}
void withdraw(double amount) {
if (amount > balance) {
cout << “Insufficient balance. Cannot withdraw “ << amount << “.” << endl;
} else {
balance -= amount;
cout << “Withdrawal of “ << amount << “ made. New balance is “ << balance << “.” << endl;}
}
double getBalance() {
return balance;
}
string getCustomerName() {
return customerName;
}
int getAccountNumber() {
return accountNumber;}
};
int main() {
BankAccount account(123456, “John Doe”, 5000.00);
account.deposit(500);
account.withdraw(200);
cout << “Account holder: “ << account.getCustomerName() << endl;
cout << “Account number: “ << account.getAccountNumber() << endl;
#include<iostream>
using namespace std;
int main() {
// Add two integers
cout << "Addition of 5 and 7: " << add(5, 7) << endl;
return 0;
}
Output
Addition of 5 and 7: 12
Addition of 5
Write a programme to demonstrate the concept of various type of constructor such
as constructor without parameter , parameterised constructor , copy constructor
and constructor with default parameter.
#include<iostream>
using namespace std;
class Person {
private:
string name;
int age;
public:
// Constructor without parameter
Person() {
name = "Unknown";
age = 0;
}
// Parameterized constructor
Person(string n, int a) {
name = n;
age = a;
}
// Copy constructor
Person(const Person &p) {
name = p.name;
age = p.age;
}
// Constructor with default parameter
Person(string n = "Unknown", int a = 0) {
name = n;
age = a;
}
// Display the details of the person
void display() {
cout << "Name: " << name << ", Age: " << age << endl;}
};
int main() {
// Creating objects using various constructors
Person p1;
Person p2("Alice", 30);
Person p3(p2);
Person p4("Bob");
// Display the details of the persons
p1.display();
p2.display();
p3.display();
p4.display();
return 0;
}
Write a programme to demonstrate the concept of static variable and static
function.
#include<iostream>
using namespace std;
public:
// Static function
static void staticFunction() {
cout << "Value of staticVar in staticFunction(): " << staticVar << endl;
}
int main() {
MyClass obj1;
MyClass obj2;
MyClass::modifyStaticVar();
return 0;
}
Write a programme to demonstrate the concept of various types of inheritance.
Single Inheritance:
#include<iostream>
using namespace std;
// Base class
class Animal {
public:
void sound() {
cout << "The animal makes a sound" << endl;}
};
// Derived class
class Cat : public Animal {
public:
void sound() {
cout << "The cat says meow" << endl;}
};
int main() {
Cat myCat;
myCat.sound();
return 0;
}
Multiple inheritance:
#include<iostream>
using namespace std;
// Base class 1
class Shape {
public:
void setWidth(int w) {
width = w;}
void setHeight(int h) {
height = h;}
protected:
int width;
int height;
};
// Base class 2
class Color {
public:
void setColor(string c) {
color = c;}
string getColor() {
return color;
}
protected:
string color;
};
// Derived class
class Circle : public Shape, public Color {
public:
void draw() {
cout << "Drawing a circle with color " << getColor() << endl;}
};
int main() {
Circle myCircle;
myCircle.setColor("Blue");
myCircle.draw();
return 0;
}
Hierarchical inheritance :
#include<iostream>
using namespace std;
// Base class 1
class Shape {
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
Write a programme to maintain the ambiguity Problem that may arise at the time of
inheritance.
#include<iostream>
class A {
protected:
int a;
public:
A(int i = 0): a(i) {}
virtual void show() {
cout << "Class A: " << a << endl}};
class B: virtual public A {
protected:
int b;
public:
B(int i = 0, int j = 0): A(i), b(j) {}
void show() {
cout << "Class B: " << b << endl;}};
class C: virtual public A {
protected:
int c;
public:
C(int i = 0, int j = 0): A(i), c(j) {}
void show() {
cout << "Class C: " << c << endl;}};
class D: public B, public C {
public:
D(int i = 0, int j = 0, int k = 0): B(i, j), C(i, k) {}
void show() {
cout << "Class D: " << endl;
B::show();
C::show();}};
int main() {
D d(1, 2, 3);
d.show();
return 0;}
Write a programme to demonstrate the concept of virtual base class.
#include<iostream>
using namespace std;
class Base1 {
public:
Base1() {
cout << "Base1 constructor called!" << endl;
}
virtual void func() {
cout << "Base1 function called!" << endl;}
};
class Base2 {
public:
Base2() {
cout << "Base2 constructor called!" << endl;}
virtual void func() {
cout << "Base2 function called!" << endl;
}
};
class Derived : public Base1, public Base2 {
public:
Derived() {
cout << "Derived constructor called!" << endl;
}
void func() {
cout << "Derived function called!" << endl;
}
};
int main() {
Derived d;
d.func();
d.Base1::func();
d.Base2::func();
return 0;
}
Output
Base1 constructor called!
Base2 constructor called!
Derived constructor called!
Derived function called!
Base1 function called!
Base2 function called!
Write a programme to demonstrate the concept of runtime polymorphism achieved
through virtual function.
#include<iostream>
using namespace std;
class Base {
public:
virtual void show() {
cout << "In Base class's show function" << endl;
}
};
int main() {
Base base;
Derived derived;
Base* bp = &base;
return 0;
}
Output
In Base class's show function
In Derived class's show function
Write a programme to create an abstract class in C++.
#include<iostream>
using namespace std;
// Abstract Class
class Shape {
public:
// Pure Virtual Function
virtual void draw() = 0;
};
int main() {
Circle circle;
Rectangle rectangle;
circle.draw();
rectangle.draw();
return 0;
}
Write a programme to create a friend function that can compare data member of
two different classes.
#include<iostream>
using namespace std;
// Class Person
class Person {
public:
string name;
int age;
int main() {
Person p1, p2;
return 0;
}
Write a programme to demonstrate the flowing operators overloading (a) increment++ (b) binary
plus (c) + & * operator simultaneously in an expression.
#include<iostream>
using namespace std;
class Counter {
private:
int count;
public:
Counter(): count(0) {}
// Prefix increment operator overloading
Counter& operator++() {
count++;
return *this;}
// Postfix increment operator overloading
Counter operator++(int) {
Counter temp = *this;
count++;
return temp;}
// Binary plus operator overloading
Counter operator+(const Counter& c) const {
Counter temp;
temp.count = count + c.count;
return temp;}
// Multiplication operator overloading
Counter operator*(const Counter& c) const {
Counter temp;
temp.count = count * c.count;
return temp;}
// Display the count value
void display() const {
cout << count << endl;}
};
int main() {
Counter c1, c2, c3;
c1.display(); // Output: 0
c2.display(); // Output: 0
// Increment operator
++c1;
c2++;
c1.display(); // Output: 1
c2.display(); // Output: 1
// Binary plus operator
c3 = c1 + c2;
c3.display(); // Output: 2
// Multiplication operator
c3 = c1 * c2;
c3.display(); // Output: 1
return 0;}