Program to demonstrate use of data members & member functions
#include <iostream>
using namespace std;
class Rectangle {
private:
int length; // private data member to store length of rectangle
int width; // private data member to store width of rectangle
public:
// constructor to initialize length and width of rectangle
Rectangle(int l, int w) {
length = l;
width = w;
}
// member function to calculate area of rectangle
int area() {
return length * width;
}
// member function to calculate perimeter of rectangle
int perimeter() {
return 2 * (length + width);
}
};
int main() {
// create an instance of Rectangle class with length 5 and width 10
Rectangle r(5, 10);
// calculate and output the area of the rectangle using the area() member function
cout << "Area of rectangle: " << r.area() << endl;
// calculate and output the perimeter of the rectangle using the perimeter() member function
cout << "Perimeter of rectangle: " << r.perimeter() << endl;
return 0;
}
Programs based on branching and looping statements using classes
1] looping
#include <iostream>
using namespace std;
class Factorial {
public:
int num;
public:
// constructor to initialize the number for which we want to calculate the factorial
Factorial(int n) {
num = n;
}
// function to calculate and return the factorial of the number
int calculateFactorial() {
int fact = 1;
for(int i=1; i<=num; i++) {
fact *= i;
}
return fact;
}
};
int main() {
// create an instance of Factorial class with number 5
Factorial f(5);
// calculate and output the factorial of the number using the calculateFactorial() member function
cout << "Factorial of " << f.num << " is " << f.calculateFactorial() << endl;
return 0;
}
2] branching
#include <iostream>
using namespace std;
class Prime {
public:
int num;
public:
// constructor to initialize the number that we want to check for primality
Prime(int n) {
num = n;
}
// function to check whether the number is prime or not and return the result as a boolean
bool isPrime() {
if(num < 2) {
return false;
}
for(int i=2; i<num; i++) {
if(num % i == 0) {
return false;
}
}
return true;
}
};
int main() {
// create an instance of Prime class with number 7
Prime p(7);
// check whether the number is prime using the isPrime() member function
if(p.isPrime()) {
cout << p.num << " is a prime number" << endl;
}
else {
cout << p.num << " is not a prime number" << endl;
}
return 0;
}
Program to demonstrate one and two dimensional arrays using classes
#include <iostream>
using namespace std;
class Array {
private:
int *arr;
int size;
public:
Array(int s) {
size = s;
arr = new int[size];
}
void set(int index, int value) {
arr[index] = value;
}
int get(int index) {
return arr[index];
}
void print() {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl<<endl;
}
};
class TwoDArray {
private:
int **arr;
int rows, cols;
public:
TwoDArray(int r, int c) {
rows = r;
cols = c;
arr = new int*[rows];
for (int i = 0; i < rows; i++) {
arr[i] = new int[cols];
}
}
void set(int row, int col, int value) {
arr[row][col] = value;
}
int get(int row, int col) {
return arr[row][col];
}
void print() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
};
int main() {
// One-dimensional array example
Array a(5);
for (int i = 0; i < 5; i++) {
a.set(i, i + 1);
}
a.print();
// Two-dimensional array example
TwoDArray b(3, 4);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
b.set(i, j, i * j);
}
}
b.print();
return 0;
}
Program to use scope resolution operator. Display the various values of the same
variables declared at different scope levels
#include <iostream>
using namespace std;
int num = 10; // Global variable
int main() {
int num = 20; // Local variable
cout << "Local num = " << num << endl;
cout << "Global num = " << ::num << endl; // Using scope resolution operator to access global variable
{
int num = 30; // Inner block variable
cout << "Inner block num = " << num << endl;
cout << "Global num = " << ::num << endl; // Using scope resolution operator to access global variable
}
cout << "Local num = " << num << endl;
cout << "Global num = " << ::num << endl; // Using scope resolution operator to access global variable
return 0;
}
Programs to demonstrate various types of constructors and destructors.
constructors (default)
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass() {
cout << "Default constructor called" << endl;
}
};
int main() {
MyClass obj; // default constructor called
return 0;
}
constructor(parameterized)
#include <iostream>
using namespace std;
class MyClass {
private:
int num;
public:
MyClass(int n) {
num = n;
}
void display() {
cout << "The value of num is: " << num << endl;
}
};
int main() {
MyClass obj(10); // parameterized constructor called with argument 10
obj.display();
return 0;
}
destructor
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass() {
cout << "Constructor called" << endl;
}
~MyClass() {
cout << "Destructor called" << endl;
}
};
int main() {
MyClass obj; // constructor called
return 0;
}
Programs to demonstrate use of public, protected & private scope specifiers.
public:
#include <iostream>
using namespace std;
class MyClass {
public:
int num;
void display() {
cout << "The value of num is: " << num << endl;
}
};
int main() {
MyClass obj;
obj.num = 10; // public member can be accessed from outside the class
obj.display();
return 0;
}
private:
#include <iostream>
using namespace std;
class MyClass {
private:
int num;
public:
void setNum(int n) {
num = n;
}
void display() {
cout << "The value of num is: " << num << endl;
}
};
int main() {
MyClass obj;
obj.setNum(10); // private member can only be accessed using public member functions
obj.display();
return 0;
}
protected:
#include <iostream>
using namespace std;
class MyBaseClass {
protected:
int num;
};
class MyDerivedClass : public MyBaseClass {
public:
void setNum(int n) {
num = n; // protected member can be accessed from derived class
}
void display() {
cout << "The value of num is: " << num << endl;
}
};
int main() {
MyDerivedClass obj;
obj.setNum(10);
obj.display();
return 0;
}
Programs to demonstrate single and multilevel inheritance
single
#include <iostream>
using namespace std;
class Shape {
protected:
int width, height;
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
};
class Rectangle: public Shape {
public:
int getArea() {
return (width * height);
}
};
int main() {
Rectangle rect;
rect.setWidth(5);
rect.setHeight(7);
cout << "The area of the rectangle is: " << rect.getArea() << endl;
return 0;
}
multilevel inheritance
#include <iostream>
using namespace std;
class Shape {
protected:
int width, height;
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
};
class Rectangle: public Shape {
public:
int getArea() {
return (width * height);
}
};
class Square: public Rectangle {
public:
int getPerimeter() {
return (2 * (width + height));
}
};
int main() {
Square sqr;
sqr.setWidth(5);
sqr.setHeight(5);
cout << "The area of the square is: " << sqr.getArea() << endl;
cout << "The perimeter of the square is: " << sqr.getPerimeter() << endl;
return 0;
}
Programs to demonstrate multiple inheritance and hierarchical inheritance
1) multiple inheritance
#include <iostream>
#include <string>
using namespace std;
class Shape {
protected:
int width, height;
public:
Shape(int w = 0, int h = 0) {
width = w;
height = h;
}
};
class Paint {
protected:
string color;
public:
Paint(string c = "white") {
color = c;
}
};
class Rectangle: public Shape, public Paint {
public:
Rectangle(int w = 0, int h = 0, string c = "white") : Shape(w, h), Paint(c) {}
int getArea() {
return (width * height);
}
string getColor() {
return color;
}
};
int main() {
Rectangle rect(5, 7, "blue");
cout << "The area of the rectangle is: " << rect.getArea() << endl;
cout << "The color of the rectangle is: " << rect.getColor() << endl;
return 0;
}
2) hierarchical inheritance
#include <iostream>
using namespace std;
class Shape {
protected:
int width, height;
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
};
class Rectangle: public Shape {
public:
int getArea() {
return (width * height);
}
};
class Triangle: public Shape {
public:
int getArea() {
return (width * height / 2);
}
};
int main() {
Rectangle rect;
Triangle tri;
rect.setWidth(5);
rect.setHeight(7);
cout << "The area of the rectangle is: " << rect.getArea() << endl;
tri.setWidth(5);
tri.setHeight(7);
cout << "The area of the triangle is: " << tri.getArea() << endl;
return 0;
}
Programs to demonstrate inheritance and derived class constructors
INHERITANCE
#include <iostream>
using namespace std;
class Shape {
protected:
int width, height;
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
};
class Rectangle: public Shape {
public:
int getArea() {
return (width * height);
}
};
int main() {
Rectangle rect;
rect.setWidth(5);
rect.setHeight(7);
cout << "The area of the rectangle is: "
<< rect.getArea() << endl;
return 0;
}
Derived Class Constructors
#include <iostream>
using namespace std;
class Shape {
protected:
int width, height;
public:
Shape(int w = 0, int h = 0) {
width = w;
height = h;
}
};
class Rectangle: public Shape {
public:
Rectangle(int w = 0, int h = 0): Shape(w, h) {}
int getArea() {
return (width * height);
}
};
int main() {
Rectangle rect(5, 7);
cout << "The area of the rectangle is: "
<< rect.getArea() << endl;
return 0;
}
Programs to demonstrate friend function, inline function, this pointer
friend
#include <iostream>
using namespace std;
class MyClass {
private:
int x;
public:
MyClass() {
x = 0;
}
friend void display(const MyClass& obj);
};
void display(const MyClass& obj) {
cout << "The value of x is: " << obj.x << endl;
}
int main() {
MyClass obj;
display(obj);
return 0;
}
inline
#include <iostream>
inline int add(int x, int y) {
return x + y;
}
int main() {
int a = 10, b = 20;
std::cout << "The sum of " << a << " and " << b << " is: "
<< add(a, b) << std::endl;
return 0;
}
pointer
#include <iostream>
using namespace std;
class MyClass {
private:
int x;
public:
MyClass(int x) {
this->x = x;
}
int getX() const {
return x;
}
void setX(int x) {
this->x = x;
}
};
int main() {
MyClass obj(10);
cout << "The value of x is: " << obj.getX() << endl;
obj.setX(20);
cout << "The value of x is now: " << obj.getX() << endl;
return 0;
}
Programs to demonstrate function overloading and overriding
overloading
#include <iostream>
using namespace std;
int add(int x, int y) {
return x + y;
}
double add(double x, double y) {
return x + y;
}
int main() {
cout << "Result of adding two integers: " << add(10, 20) << endl;
cout << "Result of adding two doubles: " << add(10.5, 20.5) << endl;
return 0;
}
overiding
#include <iostream>
using namespace std;
class Animal {
public:
virtual void makeSound() {
cout << "The animal makes a sound" << endl;
}
};
class Dog : public Animal {
public:
void makeSound() override {
cout << "The dog barks" << endl;
}
};
int main() {
Animal* animal = new Animal();
Dog* dog = new Dog();
animal->makeSound(); // call makeSound() on base class object
dog->makeSound(); // call makeSound() on derived class object
Animal* animal2 = new Dog();
animal2->makeSound(); // call makeSound() on base class pointer to derived class object
delete animal;
delete dog;
delete animal2;
return 0;
}
Programs to demonstrate use of pointer
basic
#include <iostream>
using namespace std;
int main() {
int num = 10;
int *ptr = #
cout << "Value of num: " << num << endl;
cout << "Address of num: " << &num << endl;
cout << "Value of ptr: " << ptr << endl;
cout << "Value pointed by ptr: " << *ptr << endl;
return 0;
}
pointer arithmetic
#include <iostream>
using namespace std;
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr;
for(int i=0; i<5; i++) {
cout << "Value of arr[" << i << "]: " << *(ptr+i) << endl;
}
return 0;
}
. Programs to demonstrate text and binary file handling
normal
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
// Writing to a text file
ofstream outfile;
outfile.open("textfile.txt");
outfile << "Hello, world!\n";
outfile << "This is a text file.\n";
outfile.close();
// Reading from a text file
ifstream infile;
infile.open("textfile.txt");
string line;
while (getline(infile, line)) {
cout << line << endl;
}
infile.close();
return 0;
}
binary
#include <iostream>
#include <fstream>
using namespace std;
struct Person {
string name;
int age;
};
int main() {
// Writing to a binary file
Person p = {"Alice", 25};
ofstream outfile;
outfile.open("binaryfile.bin", ios::out | ios::binary);
outfile.write((char*)&p, sizeof(p));
outfile.close();
// Reading from a binary file
Person q;
ifstream infile;
infile.open("binaryfile.bin", ios::in | ios::binary);
infile.read((char*)&q, sizeof(q));
infile.close();
cout << "Name: " << q.name << endl;
cout << "Age: " << q.age << endl;
return 0;
}