Write a programme to find sum of digits of a given number.
#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;
cout << “Current balance: “ << account.getBalance() << endl;
return 0;
}
Write a programme to demonstrate the concept of function overloading.
#include<iostream>
using namespace std;
// Function to add two integers
int add(int a, int b) {
return a + b;
}
// Function to add three integers
int add(int a, int b, int c) {
return a + b + c;
}
// Function to add two doubles
double add(double a, double b) {
return a + b;
}
int main() {
// Add two integers
cout << "Addition of 5 and 7: " << add(5, 7) << endl;
// Add three integers
cout << "Addition of 5, 7 and 10: " << add(5, 7, 10) << endl;
// Add two doubles
cout << "Addition of 5.5 and 7.7: " << add(5.5, 7.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;
// A static variable can be used across all objects of a class.
class MyClass {
private:
static int staticVar; // Static variable
public:
// Static function
static void staticFunction() {
cout << "Value of staticVar in staticFunction(): " << staticVar << endl;
}
// A regular member function
void memberFunction() {
cout << "Value of staticVar in memberFunction(): " << staticVar << endl;
}
// Static function that can access private static variable
static void modifyStaticVar() {
staticVar = 50;
}
};
// Initializing static variable
int MyClass::staticVar = 10;
int main() {
MyClass obj1;
MyClass obj2;
obj1.memberFunction(); // Output: Value of staticVar in memberFunction(): 10
obj2.memberFunction(); // Output: Value of staticVar in memberFunction(): 10
MyClass::staticFunction(); // Output: Value of staticVar in staticFunction(): 10
MyClass::modifyStaticVar();
obj1.memberFunction(); // Output: Value of staticVar in memberFunction(): 50
obj2.memberFunction(); // Output: Value of staticVar in memberFunction(): 50
MyClass::staticFunction(); // Output: Value of staticVar in staticFunction(): 50
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;
}
};
class Derived : public Base {
public:
void show() {
cout << "In Derived class's show function" << endl;
}
};
int main() {
Base base;
Derived derived;
Base* bp = &base;
Bp.show(); // It will call Base's show()
bp = &derived;
bp.show(); // It will call Derived's show()
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;
};
// Class inheriting from the Abstract Class
class Circle : public Shape {
public:
void draw() {
cout << "Drawing a circle" << endl;
}
};
// Class inheriting from the Abstract Class
class Rectangle : public Shape {
public:
void draw() {
cout << "Drawing a rectangle" << endl;
}
};
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;
// Friend function to compare age of two persons
friend bool compareAge(Person &p1, Person &p2);
};
// Friend function to compare age of two persons
bool compareAge(Person &p1, Person &p2) {
if (p1.age == p2.age)
return true;
else
return false;
}
int main() {
Person p1, p2;
// Taking details of person 1
cout << "Enter details of person 1: ";
cin >> p1.name >> p1.age;
// Taking details of person 2
cout << "Enter details of person 2: ";
cin >> p2.name >> p2.age;
// Comparing age of both persons
if (compareAge(p1, p2))
cout << "Both persons have same age." << endl;
else
cout << "Both persons have different age." << endl;
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;}