Oops
Oops
Oops
1
2
C++ Program to create multiple objects of class
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
int rollNo;
string stdName;
float perc;
};
int main()
{
cout << "This Program is written by Akhil Suryan" << "\n";
Student std1, std2;
std1.rollNo = 101;
std1.stdName = "Akhil Suryan";
std1.perc = 89.99f;
std2.rollNo = 102;
std2.stdName = "Himanshu Kumar ";
std2.perc = 93.50f;
3
return 0;
}
Output
4
C++ program to create class methods and how to access the
class methods outside of the class
#include <iostream>
using namespace std;
class Calculator {
public:
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
};
int main() {
cout << "This Program is written by Akhil Suryan" << "\n";
Calculator myCalculator;
int sum = myCalculator.add(10,20 );
int difference = myCalculator.subtract(13, 4);
std::cout << "Sum: " << sum << std::endl;
std::cout << "Difference: " << difference << std::endl;
return 0;
}
5
OUTPUT
6
C++ program to define a class method outside the class definition.
#include <iostream>
using namespace std;
class Sample {
public:
void printText1();
void printText2();
void printValue(int value);
};
void Sample::printText1()
{
cout << "Hey!!!\n";
}
void Sample::printText2()
{
cout << "Let's learn together\n";
}
void Sample::printValue(int value)
{
cout << "value is: " << value << "\n";
}
int main()
{
cout << "This program is made by Akhil Suryan\n";
Sample obj;
obj.printText1();
obj.printText2();
obj.printValue(737);
return 0;
7
Output
8
C++ program to assign values to the private data members
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
int rollNo;
string stdName;
float perc;
public:
void setValue()
{
rollNo = 73
7;
stdName = "Akhil Suryan";
perc = 98.0f;
}
void printValue()
{
cout << "Student's Roll No.: " << rollNo << "\n";
cout << "Student's Name: " << stdName << "\n";
cout << "Student's Percentage: " << perc << "\n";
}
};
int main()
{
cout << "This program is written by Akhil Suryan\n";
Student std;
std.setValue();
9
std.printValue();
return 0;
}
Output
10
#include <iostream>
using namespace std;
class Distance {
private:
int feet;
int inch;
public:
Distance();
void getDist();
void showDist();
Distance addDist(Distance d2);
Distance subDist(Distance d2);
};
Distance::Distance()
{
feet = 0;
inch = 0;
}
void Distance::getDist()
{
cout << "Enter Value of feets : ";
cin >> feet;
cout << "Enter value of inches : ";
cin >> inch;
11
Distance Distance::addDist(Distance d2)
{
Distance temp;
if (temp.inch < 0) {
temp.feet--;
temp.inch = 12 + temp.inch;
}
return temp;}
int main()
{
cout << "This program is written by Akhil Suryan\n";
Distance d1;
Distance d2;
Distance d3;
Distance d4;
12
cout << "Enter Distance2 : " << endl;
d2.getDist();
d3 = d1.addDist(d2);
Output
13
C++ program to create class to get and print details
of students
14
#include <iostream>
using namespace std;
class student {
private:
char name[30];
int rollNo;
int total;
float perc;
public:
void getDetails(void);
void putDetails(void);
};
void student::getDetails(void)
{
cout << "Enter name: ";
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter total marks outof 500: ";
cin >> total;
std.getDetails();
std.putDetails();
15
return 0;
}
Output
16
#include <iostream>
using namespace std;
class CRectArea
{
private:
int length;
int breadth;
public:
CRectArea (int,int);
int areaofrect ()
{
return (length * breadth);
}
int length1()
{
return length;
}
int breadth1()
{
return breadth;
}
};
CRectArea::CRectArea(int x, int y)
{
length = x;
breadth = y;
}
int main ()
{
CRectArea myrectangle (5,10);
cout<<"This Program is Written by Akhil Suryan\n";
cout<<"The Length of Wall :: "<<myrectangle.length1()<<"\n";
cout<<"\nThe Breadth of Wall :: "<<myrectangle.breadth1()<<"\
n";
17
cout << "\nThe area of Wall is :: " << myrectangle.areaofrect()<<
endl;
return 0;
}
Output
#include<iostream>
using namespace std;
18
class Test {
public:
int l, w, area;
Test(int length, int width) {
l = length;
w = width;
}
void findArea() {
area = l * w;
}
void display() {
cout << "Area of rectangle is:" << area;
}
};
int main() {
cout << "This program is written by Akhil Suryan\n";
int length, width;
cout << "Enter length of rectangle:";
cin >> length;
cout << "Enter width of rectangle:";
cin>>width;
Test obj(length, width);
obj.findArea();
obj.display();
return 0;
}
Output
19
C++ program to calculate the area of a wall by
copy Constructor.
#include <iostream>
using namespace std;
20
class Wall {
private:
double length;
double height;
public:
Wall(double len, double hgt) {
length = len;
height = hgt;
}
Wall(Wall &obj) {
length = obj.length;
height = obj.height;
}
double calculateArea() {
return length * height;
}
};
int main() {
Wall wall1(10.5, 8.6);
Wall wall2 = wall1;
cout << "This Program is written by Akhil Suryan\n";
cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
cout << "Area of Wall 2: " << wall2.calculateArea();
return 0;
}
OUTPUT
21
C++ Program to calculate Volume of Cube using
constructor
#include <iostream>
using namespace std;
22
class Cube {
private:
double side;
public:
Cube(double s) {
side = s;
}
double calculateVolume() {
return side * side * side;
}
};
int main() {
cout << "This Program is written by Akhil Suryan\n";
double sideLength;
std::cout << "Enter the side length of the cube: ";
std::cin >> sideLength;
Cube cube(sideLength);
return 0;
}
OUTPUT
23
C++ Program to determine the area of Rectangle using
constructor
24
#include <iostream>
using namespace std;
class Rectangle {
private:
double length;
double width;
public:
Rectangle(double len, double wid) {
length = len;
width = wid;
}
double calculateArea() {
return length * width;
}
};
int main() {
cout << "This program is written by Akhil Suryan\n";
double length, width;
std::cout << "Enter length of rectangle: ";
std::cin >> length;
std::cout << "Enter width of rectangle: ";
std::cin >> width;
Rectangle rectangle(length, width);
return 0;
}
Output
25
C++ Program to enter student details by passing
Parameters to constructors
26
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
std::string name;
int age;
double grade;
public:
Student(const std::string& n, int a, double g) {
name = n;
age = a;
grade = g;
}
void displayDetails() {
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "Grade: " << grade << std::endl;
}
};
int main() {
std::string name;
int age;
double grade;
cout << "This Program is Written by Akhil Suryan \n";
std::cout << "Enter student name: ";
std::getline(std::cin, name);
std::cout << "Enter student age: ";
std::cin >> age;
std::cout << "Enter student grade: ";
std::cin >> grade;
Student student(name, age, grade);
27
std::cout << "Student Details:" << std::endl;
student.displayDetails();
return 0;
}
Output
#include <iostream>
#include <string>
28
using namespace std;
class Student {
private:
std::string name;
int age;
double grade;
public:
Student() {
name = "Unknown";
age = 0;
grade = 0.0;
}
Student(const std::string& n) {
name = n;
age = 0;
grade = 0.0;
}
void displayDetails() {
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "Grade: " << grade << std::endl;
}
};
int main() {
cout << "This program is written by Akhil Suryan\n";
Student student1;
Student student2("Alice");
Student student3("Bob", 18, 85.5);
29
std::cout << "Student 2 Details:" << std::endl;
student2.displayDetails();
std::cout << std::endl;
return 0;
}
Output
#include <iostream>
using namespace std;
class Box {
30
private:
double length;
double width;
double height;
public:
Box(double len, double wid, double hei) {
length = len;
width = wid;
height = hei;
}
double calculateVolume() {
return length * width * height;
}
};
int main() {
cout << "This Program is written by Akhil Suryan\n";
double length, width, height;
std::cout << "Enter length of box: ";
std::cin >> length;
std::cout << "Enter width of box: ";
std::cin >> width;
std::cout << "Enter height of box: ";
std::cin >> height;
Box box(length, width, height);
std::cout << "Volume of the box is: " << box.calculateVolume() <<
std::endl;
return 0;}
Output
31
C++ Program to demonstrate example of simple inheritance
#include <iostream>
32
using namespace std;
// Base Class
class A {
public:
void Afun(void);
};
// Function definiion
void A::Afun(void)
{
cout << "I'm the body of Afun()..." << endl;
}
// Derived Class
class B : public A {
public:
void Bfun(void);
};
// Function definition
void B::Bfun(void)
{
cout << "I'm the body of Bfun()..." << endl;
}
int main()
{
// Create object of derived class - class B
B objB;
return 0;
}
Output
33
34
C++ Program to demonstrate example of private simple inheritance
#include <iostream>
using namespace std;
class A {
private:
int a;
protected:
int x;
public:
void setVal(int v)
{
x = v;
}
};
class B : private A {
public:
void printVal(void)
{
setVal(10);
cout << "value of x: " << x << endl;
}
};
int main()
{
B objB;
objB.printVal();
return 0;
}
Output
35
#include <iostream>
using namespace std;
class Base {
private:
int pvt = 1;
protected:
int prot = 2;
public:
int pub = 3;
int getPVT() {
return pvt;
}
};
class ProtectedDerived : protected Base {
public:
int getProt() {
return prot;
}
int getPub() {
return pub;
}
};
int main() {
ProtectedDerived object1;
cout << "Private cannot be accessed." << endl;
cout << "Protected = " << object1.getProt() << endl;
cout << "Public = " << object1.getPub() << endl;
return 0;
}
Output
C++ Program to read and print student’s information using two classes
and simple inheritance
#include <iostream>
36
using namespace std;
class std_basic_info {
private:
char name[30];
int age;
char gender;
public:
void getBasicInfo(void);
void putBasicInfo(void);
};
void std_basic_info::getBasicInfo(void)
{
cout << "Enter student's basic information:" << endl;
cout << "Name?: ";
cin >> name;
cout << "Age?: ";
cin >> age;
cout << "Gender?: ";
cin >> gender;
}
void std_basic_info::putBasicInfo(void)
{
cout << "Name: " << name << ",Age: " << age << ",Gender: " << gender <<
endl;
}
class std_result_info : public std_basic_info {
private:
int totalM;
float perc;
char grade;
public:
void getResultInfo(void);
void putResultInfo(void);
};
void std_result_info::getResultInfo(void)
{
cout << "Enter student's result information:" << endl;
cout << "Total Marks from 500? ";
37
cin >> totalM;
perc = (float)((totalM * 100) / 500);
cout << "Grade?: ";
cin >> grade;
}
void std_result_info::putResultInfo(void)
{
cout << "Total Marks: " << totalM << ",Percentage: " << perc << ",Grade: "
<< grade << endl;
}
int main()
{
std_result_info std;
std.getBasicInfo();
std.getResultInfo();
std.putBasicInfo();
std.putResultInfo();
return 0;
}
Output
#include <iostream>
using namespace std;
38
class A {
private:
int a;
public:
void get_a(int val_a)
{
a = val_a;
}
void disp_a(void)
{
cout << "Value of a: " << a << endl;
}
};
class B : public A {
private:
int b;
public:
void get_b(int val_a, int val_b)
{
get_a(val_a);
b = val_b;
}
void disp_b(void)
{
disp_a();
cout << "Value of b: " << b << endl;
}
};
class C : public B {
private:
int c;
public:
void get_c(int val_a, int val_b, int val_c)
{
get_b(val_a, val_b);
c = val_c;
}
void disp_c(void)
{
disp_b();
cout << "Value of c: " << c << endl;
39
}
};
int main()
{
C objC;
objC.get_c(10, 20, 30);
objC.disp_c();
return 0;
}
Output
#include <iostream>
#include <stdio.h>
40
using namespace std;
class basicInfo {
protected:
char name[30];
int empId;
char gender;
public:
void getBasicInfo(void)
{
cout << "Enter Name: ";
cin.getline(name, 30);
cout << "Enter Emp. Id: ";
cin >> empId;
cout << "Enter Gender: ";
cin >> gender;
}
};
class deptInfo {
protected:
char deptName[30];
char assignedWork[30];
int time2complete;
public:
void getDeptInfo(void)
{
cout << "Enter Department Name: ";
cin.ignore(1);
cin.getline(deptName, 30);
cout << "Enter assigned work: ";
fflush(stdin);
cin.getline(assignedWork, 30);
cout << "Enter time in hours to complete work: ";
cin >> time2complete;
}
};
class employee : private basicInfo, private deptInfo {
public:
void getEmployeeInfo(void)
{
cout << "Enter employee's basic info: " << endl;
getBasicInfo();
41
cout << "Enter employee's department info: " << endl;
getDeptInfo();
}
void printEmployeeInfo(void)
{
cout << "Employee's Information is: " << endl;
cout << "Basic Information...:" << endl;
cout << "Name: " << name << endl;
cout << "Employee ID: " << empId << endl;
cout << "Gender: " << gender << endl
<< endl;
cout << "Department Information...:" << endl;
cout << "Department Name: " << deptName << endl;
cout << "Assigned Work: " << assignedWork << endl;
cout << "Time to complete work: " << time2complete << endl;
}
};
int main()
{
employee emp;
emp.getEmployeeInfo();
emp.printEmployeeInfo();
return 0;
}
Output
42
C++ program to demonstrate example of multiple inheritance
#include <iostream>
using namespace std;
43
class A {
private:
int a;
public:
void get_a(int val_a)
{
a = val_a;
}
void put_a(void)
{
cout << "value of a: " << a << endl;
}
};
class B {
private:
int b;
public:
void get_b(int val_b)
{
b = val_b;
}
void put_b(void)
{
cout << "value of b: " << b << endl;
}
};
class C {
private:
int c;
public:
void get_c(int val_c)
{
c = val_c;
}
void put_c(void)
{
cout << "value of c: " << c << endl;
}
};
class final : public A, public B, public C {
public:
void printValues(void)
44
{
put_a();
put_b();
put_c();
}
};
int main()
{
final objFinal;
objFinal.get_a(100);
objFinal.get_b(200);
objFinal.get_c(300);
objFinal.printValues();
return 0;
}
Output
#include <iostream>
using namespace std;
45
class Number {
public:
double num;
void getNumber() {
cout << "Enter a number: ";
cin >> num;
}
};
class Square : public Number {
public:
void calculateSquare() {
cout << "Square of " << num << " is: " << num * num << endl;
}
};
class Cube : public Number {
public:
void calculateCube() {
cout << "Cube of " << num << " is: " << num * num * num << endl;
}
};
int main() {
Square squareObj;
Cube cubeObj;
squareObj.getNumber();
squareObj.calculateSquare();
cubeObj.getNumber();
cubeObj.calculateCube();
return 0;
}
Output
#include <iostream>
#include <stdio.h>
46
using namespace std;
class basicInfo {
protected:
char name[30];
int empId;
char gender;
public:
void getBasicInfo(void)
{
cout << "Enter Name: ";
cin.ignore(1);
cin.getline(name, 30);
cout << "Enter Emp. Id: ";
cin >> empId;
cout << "Enter Gender: ";
cin >> gender;
}
};
class deptInfo : private basicInfo {
protected:
char deptName[30];
char assignedWork[30];
int time2complete;
public:
void getDeptInfo(void)
{
getBasicInfo();
cout << "Enter Department Name: ";
cin.ignore(1);
cin.getline(deptName, 30);
cout << "Enter assigned work: ";
fflush(stdin);
cin.getline(assignedWork, 30);
cout << "Enter time in hours to complete work: ";
cin >> time2complete;
}
void printDeptInfo(void)
{
cout << "Employee's Information is: " << endl;
cout << "Basic Information...:" << endl;
cout << "Name: " << name << endl;
47
cout << "Employee ID: " << empId << endl;
cout << "Gender: " << gender << endl
<< endl;
cout << "Department Information...:" << endl;
cout << "Department Name: " << deptName << endl;
cout << "Assigned Work: " << assignedWork << endl;
cout << "Time to complete work: " << time2complete << endl;
}
};
class loanInfo : private basicInfo {
protected:
char loanDetails[30];
int loanAmount;
public:
void getLoanInfo(void)
{
getBasicInfo();
cout << "Enter Loan Details: ";
cin.ignore(1);
cin.getline(loanDetails, 30);
cout << "Enter loan amount: ";
cin >> loanAmount;
}
void printLoanInfo(void)
{
cout << "Employee's Information is: " << endl;
cout << "Basic Information...:" << endl;
cout << "Name: " << name << endl;
cout << "Employee ID: " << empId << endl;
cout << "Gender: " << gender << endl
<< endl;
cout << "Loan Information...:" << endl;
cout << "Loan Details: " << loanDetails << endl;
cout << "Loan Amount : " << loanAmount << endl;
}
};
int main()
{
deptInfo objD;
48
objD.getDeptInfo();
objD.printDeptInfo();
loanInfo objL;
objL.getLoanInfo();
objL.printLoanInfo();
return 0;
}
Output
49
Write a program to check number and string palindrome
#include <iostream>
#include <string>
50
using namespace std;
return true;
}
int main() {
int num;
string str;
if (isdigit(str[0])) {
num = stoi(str);
if (isPalindrome(num)) {
cout << num << " is a palindrome number." << endl;
} else {
51
cout << num << " is not a palindrome number." << endl;
}
} else {
if (isPalindrome(str)) {
cout << str << " is a palindrome string." << endl;
} else {
cout << str << " is not a palindrome string." << endl;
}
}
return 0;
}
Output
Write a program to show the effect of call by value and call by reference in
function
#include <iostream>
52
using namespace std;
void callByValue(int num) {
cout << "Inside callByValue function:" << endl;
cout << "Original value (before modification): " << num << endl;
num = 100;
cout << "Modified value (inside function): " << num << endl;
}
void callByReference(int &num) {
cout << "Inside callByReference function:" << endl;
cout << "Original value (before modification): " << num << endl;
num = 200;
cout << "Modified value (inside function): " << num << endl;
}
int main() {
int num = 42;
cout << "Original value (before function calls): " << num << endl;
callByValue(num);
cout << "Value after callByValue function: " << num << endl;
callByReference(num);
cout << "Value after callByReference function: " << num << endl;
return 0;
}
Output
53
(c) Multiplication
(d) Transpose
#include <iostream>
using namespace std;
54
for (int j = 0; j < col; j++) {
result[i][j] = mat1[i][j] - mat2[i][j];
}
}
}
int main() {
int choice;
int matrix1[MAX][MAX], matrix2[MAX][MAX], result[MAX][MAX];
int row1, col1, row2, col2, resultRow, resultCol;
55
switch (choice) {
case 1:
if (row1 == row2 && col1 == col2) {
addMatrix(matrix1, matrix2, result, row1, col1);
resultRow = row1;
resultCol = col1;
cout << "Matrix Addition Result:" << endl;
displayMatrix(result, resultRow, resultCol);
} else {
cout << "Matrix dimensions do not match for addition." << endl;
}
break;
case 2:
if (row1 == row2 && col1 == col2) {
subtractMatrix(matrix1, matrix2, result, row1, col1);
resultRow = row1;
resultCol = col1;
cout << "Matrix Subtraction Result:" << endl;
displayMatrix(result, resultRow, resultCol);
} else {
cout << "Matrix dimensions do not match for subtraction." << endl;
}
break;
case 3:
if (col1 == row2) {
multiplyMatrix(matrix1, row1, col1, matrix2, col2, result);
resultRow = row1;
resultCol = col2;
cout << "Matrix Multiplication Result:" << endl;
displayMatrix(result, resultRow, resultCol);
} else {
cout << "Matrix dimensions do not match for multiplication." <<
endl;
}
break;
case 4:
transposeMatrix(matrix1, row1, col1, result);
resultRow = col1;
resultCol = row1;
cout << "Matrix Transpose Result:" << endl;
displayMatrix(result, resultRow, resultCol);
break;
default:
56
cout << "Invalid choice." << endl;
}
return 0;
}
Output
Define a class Shape whose attributes are radius, length and width.
Calculate the perimeter of rectangle and circle. Use Constructors and
Destructors
#include <iostream>
#include <cmath>
57
class Shape {
private:
double radius;
double length;
double width;
public:
Shape(double r = 0.0, double l = 0.0, double w = 0.0) : radius(r), length(l),
width(w) {
std::cout << "Shape object created with radius=" << radius << ", length="
<< length << ", width=" << width << std::endl;
}
~Shape() {
std::cout << "Shape object destroyed." << std::endl;
}
void setAttributes(double r, double l, double w) {
radius = r;
length = l;
width = w;
}
double calculateCirclePerimeter() {
return 2 * M_PI * radius;
}
double calculateRectanglePerimeter() {
return 2 * (length + width);
}
};
int main() {
double radius, length, width;
58
double circlePerimeter = myShape.calculateCirclePerimeter();
std::cout << "Perimeter of the circle: " << circlePerimeter << std::endl;
return 0;
}
Output
Create a class person which includes: character array name of size 64, age
in numeric, character array address of size 64, and total salary in real
numbers (divide salary in different components, if required). Make an
array of objects of class Person of size 10.
(a) Write inline function that obtains the youngest and eldest age of a
person from a class person using arrays
59
(b) Write a program to develop a salary slip and display result by using
constructors
#include <iostream>
#include <cstring>
using namespace std;
class Person{
public:
char name[64],address[64];
int age,salary;
Person(char* n, char* ad, int a, int s){
strcpy(name,n);
strcpy(address,ad);
age = a;
salary = s;
cout<<"Salary Slip:" << endl << name << endl << address << endl <<
age<<endl<<salary<<endl;
}
void maxmi(int i,int * max, int *mi);
};
inline void Person:: maxmi(int i,int* max,int *mi){
if(i<*mi)
*mi = i;
if(i>*max)
*max = i;
}
int main(){
char name[64],ad[64];
int max = 0, mi = 1000,i,a,s;
Person* obj = (Person*)malloc(10*sizeof(Person));
for(i=0;i<10;i++){
cout<<"Enter name, address, age and salary"<<endl;
cin>>name>>ad>>a>>s;
obj[i]=Person(name,ad,a,s);
obj[i].maxmi(obj[i].age,&max,&mi);
}
cout<<"Youngest age = "<<mi<<endl;
cout<<"Oldest age = "<<max<<endl;
}
60
Output
61
62
Create a class called Complex for performing operations:
(a) Overload increment and decrement operations for increasing and
decreasing complex number values(Unary Operator overload)
(b) Overload ‘+’ op and ‘-‘ op for complex numbers(Binary operator
overloading)
#include <iostream>
class Complex {
private:
double real;
double imag;
public:
Complex() : real(0), imag(0) {}
Complex operator++() {
++real;
++imag;
return *this;
}
Complex operator--() {
--real;
--imag;
return *this;
}
Complex operator++(int) {
Complex temp(real, imag);
++real;
++imag;
return temp;
}
Complex operator--(int) {
Complex temp(real, imag);
--real;
--imag;
return temp;
}
63
Complex operator+(const Complex& other) {
Complex result;
result.real = real + other.real;
result.imag = imag + other.imag;
return result;
}
int main() {
Complex c1, c2, result;
std::cout << "Enter the first complex number (real imag): ";
std::cin >> c1;
std::cout << "Enter the second complex number (real imag): ";
std::cin >> c2;
result = c1 + c2;
std::cout << "c1 + c2: " << result << std::endl;
result = c1 - c2;
std::cout << "c1 - c2: " << result << std::endl;
64
++c1;
std::cout << "After prefix increment c1: " << c1 << std::endl;
--c2;
std::cout << "After prefix decrement c2: " << c2 << std::endl;
c1++;
std::cout << "After postfix increment c1: " << c1 << std::endl;
c2--;
std::cout << "After postfix decrement c2: " << c2 << std::endl;
return 0;
}
Output
65
#include <iostream>
#include <cmath>
class AreaCalculator {
public:
double AREA(double radius) {
return 3.14159 * radius * radius;
}
int main() {
AreaCalculator calc;
double radius, length, width, base, height;
std::cout << "Enter the length and width of the rectangle: ";
std::cin >> length >> width;
std::cout << "Area of the rectangle: " << calc.AREA(length, width) <<
std::endl;
std::cout << "Enter the base and height of the triangle: ";
std::cin >> base >> height;
std::cout << "Area of the triangle: " << calc.AREA(base, height, 1) <<
std::endl;
return 0;
}
Output
66
Design three classes: Student, Exam and Result. The student class has data
members such as roll no, name etc. Create a class Exam by inheriting the
67
Student class. The Exam class adds data members representing the marks
scored in six subjects. Derive the Result from class Exam and it has its own
members such as total marks. Write an interactive program to model this
relationship. What type of inheritance this model belongs to?
#include <string>
#include <iostream>
using namespace std;
class Student{
public:
int roll_no;
string name;
};
class exam:public Student{
public:
int math,english,chem,bio,phy,cse;
};
class result:public exam{
public:
int total;
};
int main()
{
result obj1;
obj1.math = 99;
obj1.english = 99;
obj1.chem = 68;
obj1.bio = 88;
obj1.phy = 80;
obj1.cse = 94;
obj1.total =
obj1.math+obj1.english+obj1.chem+obj1.bio+obj1.phy+obj1.cse;
cout<<"Total = "<<obj1.total;
}
Output
Write a program to swap two numbers (create two classes) by using Friend
function.
68
#include <iostream>
class Swap {
int temp, a, b;
public:
Swap(int a, int b)
{
this->a = a;
this->b = b;
}
friend void swap(Swap&);
};
s1.temp = s1.a;
s1.a = s1.b;
s1.b = s1.temp;
cout << "\nAfter Swapping: " << s1.a << " " << s1.b;
}
int main()
{
Swap s(4, 6);
swap(s);
return 0;
}
Output
69
Consider an example of book shop which sells books and video tapes. These
two classes are inherited from base class called media. The media class has
70
command data members such as title and publication. The book class has
data members for storing number of pages in a book and tape class has
playing time in a tape. Each class will have member functions such as read
() and show (). In the base class, these members have to be defined as
virtual functions. Write a program to models the class hierarchy for book
shop and processes objects of these classes using pointers to base class.
Write the rules of virtual functions.
#include <iostream>
#include <string>
class Media {
protected:
std::string title;
std::string publication;
public:
Media(const std::string& t, const std::string& pub) : title(t), publication(pub)
{}
public:
Book(const std::string& t, const std::string& pub, int pages) : Media(t, pub),
numPages(pages) {}
71
std::cout << "Enter number of pages: ";
std::cin >> numPages;
}
public:
Tape(const std::string& t, const std::string& pub, double time) : Media(t,
pub), playingTime(time) {}
int main() {
Media* mediaPtr = nullptr;
char choice;
std::cout << "Enter 'B' for Book or 'T' for Tape: ";
std::cin >> choice;
if (choice == 'B') {
mediaPtr = new Book("", "", 0);
} else if (choice == 'T') {
mediaPtr = new Tape("", "", 0.0);
} else {
std::cout << "Invalid choice. Exiting." << std::endl;
72
return 1;
}
mediaPtr->read();
mediaPtr->show();
delete mediaPtr;
return 0;
}
Output
Write a program to calculate the total mark of a student using the concept
of virtual base class.
73
#include <iostream>
#include <string>
class Student {
protected:
std::string name;
int rollNumber;
public:
Student(const std::string& n, int rn) : name(n), rollNumber(rn) {}
class Subject {
protected:
int marks;
public:
Subject(int m) : marks(m) {}
};
74
};
public:
Result(const std::string& n, int rn, int sm, int mm, int am)
: Student(n, rn), scienceMarks(sm), mathMarks(mm), artsMarks(am) {}
int main() {
std::string name;
int rollNumber, scienceMarks, mathMarks, artsMarks;
75
Result studentResult(name, rollNumber, 0, 0, 0);
studentResult.getMarks();
studentResult.calculateTotal();
studentResult.display();
return 0;
}
Output
Write a program to show the use of this pointer. Write the application of
this pointer.
#include <iostream>
76
class MyClass {
private:
int data;
public:
MyClass(int d) : data(d) {}
void displayData() {
std::cout << "Data: " << this->data << std::endl;
}
int main() {
MyClass obj1(42);
MyClass obj2(20);
obj1.displayData();
obj2.displayData();
if (obj1.isEqual(obj2)) {
std::cout << "Data members are equal." << std::endl;
} else {
std::cout << "Data members are not equal." << std::endl;
}
return 0;
}
Output
#include <iostream>
#include <string>
77
using namespace std;
template <class T>
class Stack
{
public:
Stack();
void push(T i);
T pop();
private:
int top;
T st[100];
};
int main ()
{
Stack<int> int_stack;
Stack<string> str_stack;
int_stack.push(67);
str_stack.push("Hello");
str_stack.push("stack using template ");
cout << int_stack.pop() << endl;
cout << str_stack.pop() << endl;
cout << str_stack.pop() << endl;
return 0;
}
78
Output
#include <iostream>
using namespace std;
79
int main()
{
int x = -1;
cout << "Before try \n";
try {
cout << "Inside try \n";
if (x < 0)
{
throw x;
cout << "After throw (Never executed) \n";
}
}
catch (int x ) {
cout << "Exception Caught \n";
}
Output
Write a program that input a file, which determines its length. Also count
the number of word occurrence. For example:” that person is going to
town to meet other person”. Here “to” and “person”-2times.
80
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
int main()
{
ifstream fin("C:\\Users\\acer\\Documents\\file4.txt"); //opening text file
int count=0;
char ch[20],c[20];
while(fin)
{
fin>>ch;
if(strcmp(ch,c)==0)
count++;
}
return 0;
Output
81