0% found this document useful (0 votes)
21 views100 pages

Oops Programs

Uploaded by

seemakasture75
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views100 pages

Oops Programs

Uploaded by

seemakasture75
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 100

#include<iostream.

h>

#include<conio.h>

#include<math.h>

void main()

float x1,x2,b,a,c,f;

clrscr();

cout<<"Enter a :";

cin>>a;

cout<<"Enter b :";

cin>>b;

cout<<"Enter c :";

cin>>c;

f=abs(sqrt((b*b)-4*a*c));

cout<<f<<endl;/*to check whether sqrt is positive*/

x1=(-b+f)/2*a;

x2=(-b-f)/2*a;

cout<<"value of x1 : "<<x1<<endl;

cout<<"value of x2 : "<<x2<<endl;

getch();

OUTPUT:
Q.2.Write a C++ program to demonstrate the use of operator precedence.

#include<iostream.h>

#include<conio.h>

#include<math.h>

void main()

float x1,x2;

clrscr();

x1=10/2*3+1-3;

x2=14+6-5*2/2;

cout<<"x1 :"<<x1<<endl<<"x2 :"<<x2<<endl;

getch();

OUTPUT:
Q.1.Write a C++ program check leap year.

#include<iostream.h>

#include<conio.h>

#include<math.h>

void main()

int year;

clrscr();

cout<<"enter year :";

cin>>year;

if(year%4==0)

cout<<year<<" is leap year";

else

cout<<year<<" is not a leap year";

getch();

OUTPUT:
Q.2.Write a C++ program armstrong numbers between two integers.

#include<iostream.h>

#include<conio.h>

#include<math.h>

void main()

int n1=0,n=0,num=0,digit=0,sum=0;

clrscr();

cout<<"enter number :";

cin>>num;

n1=num;

while(n1!=0)

n1/=10;

n++;

n1=num;
for(int i=1;i<=n;i++)

digit=n1%10;

sum=sum+digit*digit*digit;

n1=n1/10;

if(sum==num)

cout<<"Number "<<sum<<" is Armstrong"<<endl;

else

cout<<sum<<" Not a Armstrong number"<<endl;

getch();

OUTPUT:
Q.3.Write a C++ program to check whether a number is palindrome or not.

#include<iostream.h>

#include<conio.h>

void main()

int num=0,re_num=0,number=0,remainder=0;

clrscr();

cout<<"enter a number :";

cin>>num;

number=num;

while(num!=0)

remainder=num%10;

re_num=re_num*10+remainder;

num=num/10;

cout<<re_num<<endl;
if(re_num==number)

cout<<"Number is palindrome";

else

cout<<"Number is not palindrome" ;

getch();

OUTPUT:

Q.1.Write a C++ program to print a multiplication table of 7.

#include<iostream.h>

#include<conio.h>

void main()
{

clrscr();

for(int i=1;i<=10;i++)

cout<<"7X"<<i<<"="<<7*i<<endl;

getch();

OUTPUT:

Q.2.Write a C++ program to 100 to 1.

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

for(int i=100;i>=1;i--)

{
cout<<i<<"\t";

getch();

OUTPUT:

Q.3.Write a C++ program which will print (half pyramid)pattern of natural numbers

23

456

7 8 9 10

11 12 13 14 15.
#include<iostream.h>

#include<conio.h>

void main()

int i,j,n=1;

clrscr();
for(i=1;i<=5;i++)

for(j=1;j<=i;j++)

cout<<n;

n++;

cout<<endl;

getch();

OUTPUT:

Q.4.Write a C++ program; ask the user to enter the number of rows to print pyramid of stars(*)

**

***

*****
*******
#include<iostream.h>

#include<conio.h>

void main()

int i,j,row;

clrscr();

cout<<"Enter rows : ";

cin>>row;

for(i=1;i<=row;i++)

for(j=0;j<i;j++)

cout<<"*";

cout<<endl;

getch();

OUTPUT:
Q.1.Write a C++ program to find median of two sorted arrays of same size.

#include<iostream.h>

#include<conio.h>

void main()

int a[5]={10,20,30,40,50};

int b[5]={15,35,55,75,95};

int m1,m2,mid1,mid2;

clrscr();

m1=sizeof(a)/sizeof(a[0]);

m2=sizeof(b)/sizeof(b[0]);

mid1=m1/2;

mid2=m1/2;

cout<<"Middle of A :"<<a[mid1]<<endl;

cout<<"Middle of B :"<<b[mid2];

getch();

}
OUTPUT:

Q.2.Write a C++ program to find the two repeating elements in a given array.

#include<iostream.h>

#include<conio.h>

void main()

int a[5]={10,20,40,20,10},i,j;

clrscr();

for(i=0;i<2;i++)

come:

for(j=2;j<5;j++)

if(a[i]==a[j])

if(i==2&&j==2)

goto come;
}

cout<<"Repeated element :"<<a[i]<<endl;

getch();

OUTPUT:

Q.3.Find the smallest and the second smallest elements in an array.

#include<iostream.h>

#include<conio.h>

void main()

int a[5]={1,5,2,4,3};

int temp;

clrscr();

for(int i=0;i<5;i++)

{
for(int j=0;j<5;j++)

if(a[i]<a[j])

temp=a[i];

a[i]=a[j];

a[j]=temp;

for(i=0;i<2;i++)

cout<<i+1<<"number :"<<a[i]<<endl;

getch();

OUTPUT:
Q.4.Write a C++ program to find the missing number.

#include<iostream.h>

#include<conio.h>

void main()

int a[100]={1,2,3,5};

int k=5;

int total=(k)*(k+1)/2;

clrscr();

for(int i=0;i<5;i++)

total=total-a[i];

cout<<"Missing number :"<<total;

getch();

OUTPUT:
Q.1.Write a C++ program to Multiply 3*3 Matrix Using Multi-dimensional Arrays.

#include <iostream.h>

#include<conio.h>

void main()

int matrix1[3][3];

int matrix2[3][3];

int result[3][3]={0};

int i,j,k;

clrscr();

cout<<"Enter elements for Matrix 1 (3x3):"<<endl;

for(i=0;i<3;++i)

for(j=0;j<3;++j)

cout<<"Enter element at row "<<i+1<<" column "<<j+1<<":";

cin>>matrix1[i][j];

cout<<"Enter elements for Matrix 2 (3x3):"<<endl;

for(i=0;i<3;++i)

for(j=0;j<3;++j)

cout<<"Enter element at row "<<i+1<<" column "<<j+1<<": ";


cin>>matrix2[i][j];

for(i=0;i<3;++i)

for(j=0;j<3;++j)

for(k=0;k<3;++k)

result[i][j]+=matrix1[i][k]*matrix2[k][j];

cout<<"Resultant Matrix:"<<endl;

for(i=0;i<3;++i)

for(j=0;j<3;++j)

cout<<result[i][j]<<" ";

cout<<endl;

getch();

OUTPUT:
Q.2.Write a C++ program to find Transpose of 2*2 matrix.

#include <iostream.h>

#include<conio.h>

void main()

int matrix[2][2];

int transpose[2][2];

int i,j;

clrscr();

cout<< "Enter elements for the 2x2 matrix:"<<endl;

for(i=0;i<2;++i)

for(j=0;j<2;++j)

cout<<"Enter element at row "<<i+1<<" column "<<j+1<<": ";

cin>>matrix[i][j];

}
for(i=0;i<2;++i)

for(j=0;j<2;++j)

transpose[j][i]=matrix[i][j];

cout<<"Transpose of the matrix:"<<endl;

for(i=0;i<2;++i)

for(j=0;j<2;++j)

cout<<transpose[i][j]<<" ";

cout<<endl;

getch();

OUTPUT:
Q.1.Write a C++ program to declare a class “Book” having data members book_name,author and
price.Accept and display data for book having maximum price.

#include<iostream.h>

#include<string.h>

#include<conio.h>

#include<stdio.h>

class Book

public:

char book_name[30];

char author[30];

double price;

void read()

cout<<"Book Name: ";

cin>>book_name;

cout<< "Author: ";


cin>>author;

cout<<"Price: ";

cin>>price;

void display()

cout<<"Book Name: "<<book_name<<endl;

cout<< "Author: "<<author<<endl;

cout<<"Price: "<<price<<"$"<<endl;

double getPrice()

return price;

};

void main()

int num,i=1,j=0,max;

clrscr();

cout<<"Enter number of books :";

cin>>num;

Book book[100];

cout<<"Enter information of "<<num<<" books"<<endl;

for(i=0;i<num;i++)

book[i].read();

for(i=0;i<num;i++)
{

for(j=0;j<num;j++)

if(book[i].price>book[j+1].price)

max=i;

break;

cout<<"Information of book with highest price :"<<endl;

book[max].display();

getch();

OUTPUT:

Q.2.Write a C++ program to declare a class “Staff” having data members name,basic salary,DA,HRA
and calculate gross salary.Accept and display data for one staff.
a)Where DA=74.5% of basic

1) HRA=30% of basic

2)Gross_salary=basic+HRA+DA

#include<iostream.h>

#include<conio.h>

class Staff

public:

char name[20];

float HRA,DA,sal,gr_sal;

void accept()

cout<<"Name : ";

cin>>name;

cout<<"enter salary : ";

cin>>sal;

void display()

DA=(sal*74.5)/100;

HRA=(sal*30)/100;

gr_sal=sal+HRA+DA;

cout<<"Staff Name : "<<name<<endl;

cout<<"Salary : "<<sal<<endl;

cout<<"HRA : "<<HRA<<endl;

cout<<"DA : "<<DA<<endl;

cout<<"Gross Salary : "<<gr_sal<<endl;

}
};

void main()

clrscr();

Staff s;

s.accept();

s.display();

getch();

OUTPUT:

PRACTICAL NO.7

#include <iostream.h>

#include <conio.h> // For Turbo C++ compatibility


class Employee {

public:

int emp_id;

char emp_name[50];

float emp_salary;

void getData() {

cout << "Enter Employee ID: ";

cin >> emp_id;

cout << "Enter Employee Name: ";

cin.ignore(); // Clear the input buffer

cin.getline(emp_name, 50);

cout << "Enter Employee Salary: ";

cin >> emp_salary;

void displayData() {

cout << "Employee ID: " << emp_id << endl;

cout << "Employee Name: " << emp_name <<endl;

cout << "Employee Salary: " << emp_salary <<endl;

};

int main() {

clrscr();

int n;

cout << "Enter the number of employees: ";


cin >> n;

Employee employees[100];

for (int i = 0; i < n; i++) {

cout << "\nEnter details for Employee " << (i + 1) << ":\n";

employees[i].getData();

cout << "\nEmployees with salary greater than 25,000/-:\n";

for (i = 0; i < n; i++) {

if (employees[i].emp_salary > 25000) {

employees[i].displayData();

getch();

return 0;

OUTPUT:
#include <iostream.h>

#include <conio.h> // For Turbo C++ compatibility

class City {

private:

char name[50];

int population;

public:

void getData() {

cout << "Enter City Name: ";

cin.ignore(); // Clear the input buffer

cin.getline(name, 50);

cout << "Enter City Population: ";

cin >> population;


}

void displayData() {

cout << "City Name: " << name <<endl ;

cout << "City Population: " << population <<endl ;

};

int main() {

clrscr();

City cities[10];

for (int i = 0; i < 10; i++) {

cout << "\nEnter details for City " << (i + 1) << ":\n";

cities[i].getData();

cout << "\nCity data for 10 cities:\n";

for (i = 0; i < 10; i++) {

cout << "\nDetails for City " << (i + 1) << ":\n";

cities[i].displayData();

getch(); // Wait for a key press (Turbo C++ specific)

return 0;

OUTPUT:
Write a C++ program to swap the values of two variables using friend functions.

#include <iostream.h>

#include <conio.h>

class Swap; // Forward declaration of the Swap class

class Number {
private:

int value;

public:

Number(int val) : value(val) {}

friend void swapNumbers(Number &num1, Number &num2);

friend void display(Number &num);

};

void swapNumbers(Number &num1, Number &num2) {

int temp = num1.value;

num1.value = num2.value;

num2.value = temp;

void display(Number &num) {

cout << "Value: " << num.value << endl;

int main() {

clrscr(); // Clear the screen (Turbo C++ specific)

Number num1(5);

Number num2(10);

cout << "Before swapping:\n";


cout << "Number 1: ";

display(num1);

cout << "Number 2: ";

display(num2);

swapNumbers(num1, num2);

cout << "\nAfter swapping:\n";

cout << "Number 1: ";

display(num1);

cout << "Number 2: ";

display(num2);

getch(); // Wait for a key press (Turbo C++ specific)

return 0;

OUTPUT:
Write a C++ program to define a class “Distance” having data member as meters.Display the
addition of two Distances objects using friend function.

#include <iostream.h>

#include <conio.h>

class Distance {

private:

int meters;

public:

Distance(int m) : meters(m) {}

// Declare a friend function for addition

friend Distance addDistances(Distance d1, Distance d2);

void display() {

cout << "Meters: " << meters << " m" << endl;

}
};

// Define the friend function to add two Distance objects

Distance addDistances(Distance d1, Distance d2) {

int totalMeters = d1.meters + d2.meters;

Distance result(totalMeters);

return result;

int main() {

clrscr(); // Clear the screen (Turbo C++ specific)

Distance distance1(10);

Distance distance2(20);

cout << "Distance 1: ";

distance1.display();

cout << "Distance 2: ";

distance2.display();

Distance sum = addDistances(distance1, distance2);

cout << "\nSum of Distances: ";

sum.display();

getch(); // Wait for a key press (Turbo C++ specific)

return 0;
}

OUTPUT:

#include <iostream.h>

#include <conio.h>

class Number {

private:

int x, y;

public:

Number(int a, int b) : x(a), y(b) {}

// Inline function for addition

inline int add() {

return x + y;
}

// Inline function for subtraction

inline int subtract() {

return x - y;

// Inline function for multiplication

inline int multiply() {

return x * y;

// Inline function for division

inline float divide() {

if (y == 0) {

cout << "Error: Division by zero is not allowed." << endl;

return 0;

return (float)x / y;

};

int main() {

clrscr(); // Clear the screen (Turbo C++ specific)

int a, b;

cout << "Enter the first number (x): ";


cin >> a;

cout << "Enter the second number (y): ";

cin >> b;

Number num(a, b);

cout << "Addition (x + y): " << num.add() << endl;

cout << "Subtraction (x - y): " << num.subtract() << endl;

cout << "Multiplication (x * y): " << num.multiply() << endl;

cout << "Division (x / y): " << num.divide() << endl;

getch(); // Wait for a key press (Turbo C++ specific)

return 0;

OUTPUT:
#include <iostream.h>

#include <conio.h>

class Number {

private:

int x, y;

public:

Number(int a, int b) : x(a), y(b) {

cout << "Constructor is called." << endl;

~Number() {

cout << "Destructor is called." << endl;

int add() {

return x + y;

int subtract() {

return x - y;

int multiply() {
return x * y;

float divide() {

if (y == 0) {

cout << "Error: Division by zero is not allowed." << endl;

return 0;

return (float)x / y;

};

int main() {

clrscr(); // Clear the screen (Turbo C++ specific)

int a, b;

cout << "Enter the first number (x): ";

cin >> a;

cout << "Enter the second number (y): ";

cin >> b;

Number num(a, b);

cout << "Addition (x + y): " << num.add() << endl;

cout << "Subtraction (x - y): " << num.subtract() << endl;

cout << "Multiplication (x * y): " << num.multiply() << endl;

cout << "Division (x / y): " << num.divide() << endl;


getch(); // Wait for a key press (Turbo C++ specific)

return 0;

OUTPUT:

Write a C++ program to define a class “product” having data members prod_id, prod_name and prod_price.
Accept and display data for 3 products using constructor overloading.

#include<iostream.h>

#include<conio.h>

#include<string.h>

class Product {

private:

int prod_id;

char prod_name[50];

float prod_price;

public:

// Default constructor
Product() {

prod_id = 0;

prod_name[0] = '\0';

prod_price = 0.0;

// Parameterized constructor with 3 parameters

Product(int id, const char name[], float price) {

prod_id = id;

strcpy(prod_name, name);

prod_price = price;

// Member function to accept product data

void acceptData() {

cout << "Enter Product ID: ";

cin >> prod_id;

cout << "Enter Product Name: ";

cin.ignore();

cin.getline(prod_name, 50);

cout << "Enter Product Price: ";

cin >> prod_price;

// Member function to display product data

void displayData() {

cout << "Product ID: " << prod_id << endl;

cout << "Product Name: " << prod_name << endl;


cout << "Product Price: $" << prod_price << endl;

};

int main() {

clrscr();

Product products[3]; // Create an array of 3 products

// Accept data for 3 products using a loop

for (int i = 0; i < 3; i++) {

cout << "Enter details for Product " << i + 1 << ":" << endl;

products[i].acceptData();

cout << "\nDisplaying Product Details:" << endl;

// Display data for all 3 products

for (i=0; i < 3; i++) {

cout << "\nProduct " << i + 1 << " details:" << endl;

products[i].displayData();

getch();

return 0;

OUTPUT:
#include<iostream.h>

#include<conio.h>

#include<string.h>

class Student {

protected:

int roll_no;

char name[50];

public:

void acceptStudentData() {

cout << "Enter Roll Number: ";

cin >> roll_no;

cout << "Enter Name: ";

cin.ignore();
cin.getline(name, 50);

void displayStudentData() {

cout << "Roll Number: " << roll_no << endl;

cout << "Name: " << name << endl;

};

class Marks : public Student {

private:

float m1, m2, m3;

float total;

float percentage;

public:

void acceptMarksData() {

cout << "Enter Marks for Subject 1: ";

cin >> m1;

cout << "Enter Marks for Subject 2: ";

cin >> m2;

cout << "Enter Marks for Subject 3: ";

cin >> m3;

// Calculate total and percentage

total = m1 + m2 + m3;

percentage = (total / 300) * 100;


}

void displayMarksData() {

cout << "Marks for Subject 1: " << m1 << endl;

cout << "Marks for Subject 2: " << m2 << endl;

cout << "Marks for Subject 3: " << m3 << endl;

cout << "Total Marks: " << total << endl;

cout << "Percentage: " << percentage << "%" << endl;

};

void main() {

clrscr();

Marks studentData; // Create an object of the Marks class

cout << "Enter Student Details:" << endl;

studentData.acceptStudentData();

cout << "Enter Marks Details:" << endl;

studentData.acceptMarksData();

cout << "\nDisplaying Student Details:" << endl;

studentData.displayStudentData();

cout << "\nDisplaying Marks Details:" << endl;

studentData.displayMarksData();
getch();

OUTPUT:

Write a c++ to define a class data members

#include<iostream.h>

#include<conio.h>

#include<string.h>

class Employee {

protected:

int emp_no;

char emp_name[50];

char emp_designation[50];

public:
void acceptEmployeeData() {

cout << "Enter Employee Number: ";

cin >> emp_no;

cout << "Enter Employee Name: ";

cin.ignore();

cin.getline(emp_name, 50);

cout << "Enter Employee Designation: ";

cin.getline(emp_designation, 50);

void displayEmployeeData() {

cout << "Employee Number: " << emp_no << endl;

cout << "Employee Name: " << emp_name << endl;

cout << "Employee Designation: " << emp_designation << endl;

};

class Salary : public Employee {

private:

float hra, da, basic, gross_sal;

public:

void acceptSalaryData() {

cout << "Enter HRA: ";

cin >> hra;

cout << "Enter DA: ";

cin >> da;

cout << "Enter Basic Salary: ";

cin >> basic;

// Calculate gross salary


gross_sal = basic + hra + da;

void displaySalaryData() {

cout << "HRA: " << hra << endl;

cout << "DA: " << da << endl;

cout << "Basic Salary: " << basic << endl;

cout << "Gross Salary: " << gross_sal << endl;

};

void main() {

clrscr();

Salary employeeData; // Create an object of the Salary class

cout << "Enter Employee Details:" << endl;

employeeData.acceptEmployeeData();

cout << "Enter Salary Details:" << endl;

employeeData.acceptSalaryData();

cout << "\nDisplaying Employee Details:" << endl;

employeeData.displayEmployeeData();

cout << "\nDisplaying Salary Details:" << endl;

employeeData.displaySalaryData();

getch();

}
OUTPUT:

#include<iostream.h>

#include<conio.h>

#include<string.h>

class Person {
protected:

char name[50];

char gender[10];

int age;

public:

void acceptPersonData() {

cout << "Enter Name: ";

cin.ignore();

cin.getline(name, 50);

cout << "Enter Gender: ";

cin.getline(gender, 10);

cout << "Enter Age: ";

cin >> age;

void displayPersonData() {

cout << "Name: " << name << endl;

cout << "Gender: " << gender << endl;

cout << "Age: " << age << endl;

};

class Employee : public Person {

protected:

int emp_id;

char company[50];
float salary;

public:

void acceptEmployeeData() {

cout << "Enter Employee ID: ";

cin >> emp_id;

cout << "Enter Company: ";

cin.ignore();

cin.getline(company, 50);

cout << "Enter Salary: ";

cin >> salary;

void displayEmployeeData() {

cout << "Employee ID: " << emp_id << endl;

cout << "Company: " << company << endl;

cout << "Salary: " << salary << endl;

};

class Programmer : public Employee {

private:

int no_of_prog_lang_known;

public:

void acceptProgrammerData() {

cout << "Enter Number of Programming Languages Known: ";


cin >> no_of_prog_lang_known;

void displayProgrammerData() {

cout << "Number of Programming Languages Known: " << no_of_prog_lang_known << endl;

};

void main() {

clrscr();

Programmer programmerData; // Create an object of the Programmer class

cout << "Enter Person Details:" << endl;

programmerData.acceptPersonData();

cout << "Enter Employee Details:" << endl;

programmerData.acceptEmployeeData();

cout << "Enter Programmer Details:" << endl;

programmerData.acceptProgrammerData();

cout << "\nDisplaying Person Details:" << endl;

programmerData.displayPersonData();

cout << "\nDisplaying Employee Details:" << endl;

programmerData.displayEmployeeData();

cout << "\nDisplaying Programmer Details:" << endl;


programmerData.displayProgrammerData();

getch();

OUTPUT:

#include<iostream.h>
#include<conio.h>

#include<string.h>

class Car {

protected:

char car_type[50];

public:

void acceptCarData() {

cout << "Enter Car Type: ";

cin.ignore();

cin.getline(car_type, 50);

void displayCarData() {

cout << "Car Type: " << car_type << endl;

};

class Brand {

protected:

char brand_name[50];

int speed;

public:

void acceptBrandData() {

cout << "Enter Brand Name: ";


cin.ignore();

cin.getline(brand_name, 50);

cout << "Enter Speed: ";

cin >> speed;

void displayBrandData() {

cout << "Brand Name: " << brand_name << endl;

cout << "Speed: " << speed << " mph" << endl;

};

class Model {

protected:

char model_name[50];

float price;

public:

void acceptModelData() {

cout << "Enter Model Name: ";

cin.ignore();

cin.getline(model_name, 50);

cout << "Enter Price: $";

cin >> price;

void displayModelData() {
cout << "Model Name: " << model_name << endl;

cout << "Price: $" << price << endl;

};

class CarDetails : public Car, public Brand, public Model {

public:

void acceptCarDetails() {

cout << "Enter Car Details:" << endl;

acceptCarData();

acceptBrandData();

acceptModelData();

void displayCarDetails() {

cout << "\nDisplaying Car Details:" << endl;

displayCarData();

displayBrandData();

displayModelData();

};

void main() {

clrscr();

CarDetails carInfo; // Create an object of the CarDetails class


carInfo.acceptCarDetails();

carInfo.displayCarDetails();

getch();

OUTPUT:

PRACTICAL NO.13
#include<iostream.h>

#include<conio.h>

class Rectangle {

protected:

float length;

float width;

public:

void acceptDimensions() {

cout << "Enter length of the rectangle: ";

cin >> length;

cout << "Enter width of the rectangle: ";

cin >> width;

};

class Area {

protected:

float area;

public:

void calculateArea(float l, float w) {

area = l * w;

void displayArea() {
cout << "Area of the rectangle: " << area << endl;

};

class Perimeter {

protected:

float perimeter;

public:

void calculatePerimeter(float l, float w) {

perimeter = 2 * (l + w);

void displayPerimeter() {

cout << "Perimeter of the rectangle: " << perimeter << endl;

};

class RectangleDetails : public Rectangle, public Area, public Perimeter {

public:

void calculateAndDisplay() {

calculateArea(length, width);

displayArea();

calculatePerimeter(length, width);

displayPerimeter();

};
void main() {

clrscr();

RectangleDetails rectInfo; // Create an object of the RectangleDetails class

rectInfo.acceptDimensions();

rectInfo.calculateAndDisplay();

getch();

OUTPUT:

Write a C++ program for presentation of class hierarchy as below assume suitable data and function
members.
#include<iostream.h>

#include<conio.h>

#include<string.h>

class Cricketer {

protected:

char name[50];

int age;

public:

Cricketer(const char* _name, int _age) : age(_age) {

strncpy(name, _name, 50);

void displayDetails() {

cout << "Name: " << name << endl;

cout << "Age: " << age << " years" << endl;

};
class Bowler : public Cricketer {

protected:

int wickets;

public:

Bowler(const char* _name, int _age, int _wickets) : Cricketer(_name, _age), wickets(_wickets) {}

void displayBowlerDetails() {

cout << "Wickets Taken: " << wickets << endl;

};

class Batsman : public Cricketer {

protected:

int runsScored;

public:

Batsman(const char* _name, int _age, int _runsScored) : Cricketer(_name, _age),


runsScored(_runsScored) {}

void displayBatsmanDetails() {

cout << "Runs Scored: " << runsScored << endl;

};

class AllRounder : public Bowler, public Batsman {

public:
AllRounder(const char* _name, int _age, int _wickets, int _runsScored)

: Bowler(_name, _age, _wickets), Batsman(_name, _age, _runsScored) {}

void displayAllRounderDetails() {

cout << "Wickets Taken: " << wickets << endl;

cout << "Runs Scored: " << runsScored << endl;

};

void main() {

clrscr();

Bowler bowler("James Anderson", 39, 617);

Batsman batsman("Virat Kohli", 33, 7540);

AllRounder allRounder("Shakib Al Hasan", 34, 225, 3861);

cout << "Bowler's Details:" << endl;

bowler.displayBowlerDetails();

cout << "\nBatsman's Details:" << endl;

batsman.displayBatsmanDetails();

cout << "\nAll-Rounder's Details:" << endl;

allRounder.displayAllRounderDetails();

getch();

OUTPUT:
PRACTICAL NO.14
Write a C++ declare a class members

#include <iostream.h>

#include <conio.h>

class Book {

private:

char book_name[50];

char author_name[50];

float price;

public:

void input() {

cout << "Enter Book Name: ";

cin.getline(book_name, 50);
cout << "Enter Author Name: ";

cin.getline(author_name, 50);

cout << "Enter Price: ";

cin >> price;

void display() {

cout << "Book Name: " << book_name << endl;

cout << "Author Name: " << author_name << endl;

cout << "Price: " << price << endl;

};

int main() {

clrscr();

// Create a pointer to a Book object

Book* bookPtr = new Book;

cout << "Enter book details:\n";

bookPtr->input();

cout << "\nBook Details:\n";

bookPtr->display();
delete bookPtr; // Free the memory

getch();

return 0;

OUTPUT:

#include <iostream.h>

#include <conio.h>

class Box {

private:

float height;

float width;
float breadth;

public:

void input() {

cout << "Enter Height: ";

cin >> height;

cout << "Enter Width: ";

cin >> width;

cout << "Enter Breadth: ";

cin >> breadth;

void display() {

cout << "Height: " << height << endl;

cout << "Width: " << width << endl;

cout << "Breadth: " << breadth << endl;

float calculateVolume() {

return height * width * breadth;

float calculateArea() {

return 2 * (height * width + width * breadth + height * breadth);

}
};

int main() {

clrscr();

// Create a pointer to a Box object

Box* boxPtr = new Box;

cout << "Enter box dimensions:\n";

boxPtr->input();

cout << "\nBox Dimensions:\n";

boxPtr->display();

float volume = boxPtr->calculateVolume();

float area = boxPtr->calculateArea();

cout << "\nVolume: " << volume << endl;

cout << "Surface Area: " << area << endl;

delete boxPtr; // Free the memory

getch();

return 0;

}
OUTPUT:

#include <iostream.h>

#include <conio.h>

class Birthday {

private:

int day;

int month;

int year;

public:

void input() {

cout << "Enter Day: ";

cin >> day;


cout << "Enter Month: ";

cin >> month;

cout << "Enter Year: ";

cin >> year;

void display() {

cout << "Day: " << day << endl;

cout << "Month: " << month << endl;

cout << "Year: " << year << endl;

};

int main() {

clrscr();

const int numObjects = 5;

Birthday* birthdayArray = new Birthday[numObjects];

cout << "Enter birthday information for five people:\n";

for (int i = 0; i < numObjects; i++) {

cout << "\nPerson " << i + 1 << ":\n";

birthdayArray[i].input();

cout << "\nBirthday Information:\n";


for (int i = 0; i < numObjects; i++) {

cout << "\nPerson " << i + 1 << ":\n";

birthdayArray[i].display();

delete[] birthdayArray; // Free the memory

getch();

return 0;

OUTPUT:
#include <iostream.h>

#include <conio.h>

class Polygon {

protected:

float width;

float height;

public:

Polygon(float w, float h) : width(w), height(h) {}

virtual float area() {

return 0.0;

};
class Rectangle : public Polygon {

public:

Rectangle(float w, float h) : Polygon(w, h) {}

float area() {

return width * height;

};

class Triangle : public Polygon {

public:

Triangle(float w, float h) : Polygon(w, h) {}

float area() {

return 0.5 * width * height;

};

int main() {

clrscr();

// Create a pointer to a Rectangle object

Polygon* rectPtr = new Rectangle(5.0, 4.0);

// Create a pointer to a Triangle object

Polygon* triPtr = new Triangle(6.0, 3.0);


// Calculate and display the areas

cout << "Area of Rectangle: " << rectPtr->area() << endl;

cout << "Area of Triangle: " << triPtr->area() << endl;

// Free the memory

delete rectPtr;

delete triPtr;

getch();

return 0;

OUTPUT:

#include <iostream.h>

#include <conio.h>
class Counter {

private:

int count;

public:

Counter() : count(0) {}

void display() {

cout << "Count: " << count << endl;

// Overload the ++ operator (prefix)

Counter& operator++() {

count++;

return *this;

// Overload the -- operator (prefix)

Counter& operator--() {

count--;

return *this;

// Overload the ++ operator (postfix)

Counter operator++(int) {

Counter temp = *this;

count++;
return temp;

// Overload the -- operator (postfix)

Counter operator--(int) {

Counter temp = *this;

count--;

return temp;

};

int main() {

clrscr();

Counter c;

c.display();

// Testing the increment (++) operator

c++;

c.display();

// Testing the decrement (--) operator

c--;

c.display();

// Testing the increment (++) operator (prefix)

++c;
c.display();

// Testing the decrement (--) operator (prefix)

--c;

c.display();

getch();

return 0;

OUTPUT:

#include <iostream.h>

#include <conio.h>

class Complex {

private:

float real;
float imag;

public:

Complex() : real(0.0), imag(0.0) {}

Complex(float r, float i) : real(r), imag(i) {}

void display() {

cout << real;

if (imag >= 0)

cout << " + " << imag << "i";

else

cout << " - " << -imag << "i";

cout << endl;

// Declare the friend function for addition

friend Complex operator+(const Complex& c1, const Complex& c2);

};

// Define the friend function to overload the + operator

Complex operator+(const Complex& c1, const Complex& c2) {

return Complex(c1.real + c2.real, c1.imag + c2.imag);

int main() {

clrscr();
Complex num1(3.0, 4.0); // 3 + 4i

Complex num2(1.5, 2.5); // 1.5 + 2.5i

Complex sum = num1 + num2; // Adding two complex numbers

cout << "Number 1: ";

num1.display();

cout << "Number 2: ";

num2.display();

cout << "Sum: ";

sum.display();

getch();

return 0;

OUTPUT:
#include <iostream.h>

#include <conio.h>

class Binary {

private:

float value;

public:

Binary() : value(0.0) {}

Binary(float v) : value(v) {}

void display() {

cout << "Value: " << value << endl;

// Overload the addition (+) operator


Binary operator+(const Binary& other) {

return Binary(value + other.value);

// Overload the subtraction (-) operator

Binary operator-(const Binary& other) {

return Binary(value - other.value);

// Overload the multiplication (*) operator

Binary operator*(const Binary& other) {

return Binary(value * other.value);

// Overload the division (/) operator

Binary operator/(const Binary& other) {

if (other.value != 0.0)

return Binary(value / other.value);

else {

cout << "Division by zero is not allowed." << endl;

return Binary();

};

int main() {

clrscr();
Binary num1(5.0);

Binary num2(3.0);

Binary result1 = num1 + num2;

Binary result2 = num1 - num2;

Binary result3 = num1 * num2;

Binary result4 = num1 / num2;

cout << "Number 1: ";

num1.display();

cout << "Number 2: ";

num2.display();

cout << "Addition: ";

result1.display();

cout << "Subtraction: ";

result2.display();

cout << "Multiplication: ";

result3.display();

cout << "Division: ";

result4.display();
getch();

return 0;

OUTPUT:

#include <iostream.h>

#include <conio.h>

#include <string.h>

class StringCompare {

private:

char* str;

public:

StringCompare(const char* s) {

str = new char[strlen(s) + 1];

strcpy(str, s);
}

// Overload the == operator

int operator==(const StringCompare& other) {

return strcmp(str, other.str) == 0;

// Destructor to free memory

~StringCompare() {

delete[] str;

};

int main() {

clrscr();

StringCompare str1("Hello");

StringCompare str2("World");

StringCompare str3("Hello");

if (str1 == str2) {

cout << "str1 is equal to str2" << endl;

} else {

cout << "str1 is not equal to str2" << endl;

if (str1 == str3) {
cout << "str1 is equal to str3" << endl;

} else {

cout << "str1 is not equal to str3" << endl;

getch();

return 0;

OUTPUT:

#include <iostream.h>

#include <conio.h>

// Function to interchange two integers

void swap(int& a, int& b) {

int temp = a;
a = b;

b = temp;

// Function to interchange two floats

void swap(float& a, float& b) {

float temp = a;

a = b;

b = temp;

// Function to interchange two characters

void swap(char& a, char& b) {

char temp = a;

a = b;

b = temp;

int main() {

clrscr();

int int1 = 5, int2 = 10;

float float1 = 2.5, float2 = 7.3;

char char1 = 'A', char2 = 'B';

cout << "Before swapping:\n";

cout << "int1: " << int1 << " int2: " << int2 << endl;
cout << "float1: " << float1 << " float2: " << float2 << endl;

cout << "char1: " << char1 << " char2: " << char2 << endl;

swap(int1, int2);

swap(float1, float2);

swap(char1, char2);

cout << "\nAfter swapping:\n";

cout << "int1: " << int1 << " int2: " << int2 << endl;

cout << "float1: " << float1 << " float2: " << float2 << endl;

cout << "char1: " << char1 << " char2: " << char2 << endl;

getch();

return 0;

OUTPUT:
Write a C++ program that find the distance between two points in 2d and 3d space using function
overloading

#include <iostream.h>

#include <conio.h>

#include <math.h>

class Point2D {

private:

float x;

float y;

public:

Point2D(float x, float y) : x(x), y(y) {}

float distance(const Point2D& other) {

float dx = x - other.x;

float dy = y - other.y;

return sqrt(dx * dx + dy * dy);

};

class Point3D {

private:

float x;

float y;

float z;
public:

Point3D(float x, float y, float z) : x(x), y(y), z(z) {}

float distance(const Point3D& other) {

float dx = x - other.x;

float dy = y - other.y;

float dz = z - other.z;

return sqrt(dx * dx + dy * dy + dz * dz);

};

int main() {

clrscr();

Point2D point2D1(1.0, 2.0);

Point2D point2D2(4.0, 6.0);

Point3D point3D1(1.0, 2.0, 3.0);

Point3D point3D2(4.0, 6.0, 9.0);

float distance2D = point2D1.distance(point2D2);

float distance3D = point3D1.distance(point3D2);

cout << "Distance in 2D space: " << distance2D << endl;

cout << "Distance in 3D space: " << distance3D << endl;

getch();
return 0;

OUTPUT:

#include <iostream.h>
#include <conio.h>
#include <math.h>

const float PI = 3.14159265358979323846;

class AreaCalculator {
public:
// Calculate the area of a square
float calculateSquareArea(float side) {
return side * side;
}

// Calculate the area of a rectangle


float calculateRectangleArea(float length, float width) {
return length * width;
}

// Calculate the area of a circle


float calculateCircleArea(float radius) {
return PI * radius * radius;
}
// Calculate the area of a triangle
float calculateTriangleArea(float base, float height) {
return 0.5 * base * height;
}

// Calculate the area of a trapezoid


float calculateTrapezoidArea(float base1, float base2, float height) {
return 0.5 * (base1 + base2) * height;
}
};

int main() {
clrscr();

AreaCalculator calculator;

// Square
float squareArea = calculator.calculateSquareArea(5.0);
cout << "Area of Square: " << squareArea << endl;

// Rectangle
float rectangleArea = calculator.calculateRectangleArea(6.0, 4.0);
cout << "Area of Rectangle: " << rectangleArea << endl;

// Circle
float circleArea = calculator.calculateCircleArea(3.0);
cout << "Area of Circle: " << circleArea << endl;

// Triangle
float triangleArea = calculator.calculateTriangleArea(4.0, 3.0);
cout << "Area of Triangle: " << triangleArea << endl;

// Trapezoid
float trapezoidArea = calculator.calculateTrapezoidArea(2.0, 6.0, 4.0);
cout << "Area of Trapezoid: " << trapezoidArea << endl;

getch();
return 0;
}
OUTPUT:

Write a C++ program ask to the user to enter file name to encrypt its content.

#include <iostream.h>

#include <fstream.h>

#include <conio.h>

#include <ctype.h> // For isalpha and islower

void encryptFileContent(const char* filename) {

ifstream inputFile(filename);

if (!inputFile) {

cout << "Unable to open the file: " << filename << endl;

return;

}
ofstream outputFile("encrypted.txt");

if (!outputFile) {

cout << "Unable to create the output file." << endl;

return;

char ch;

const int shift = 3;

while (inputFile.get(ch)) {

if (isalpha(ch)) {

char base = (islower(ch)) ? 'a' : 'A';

ch = char(((ch - base + shift) % 26) + base);

outputFile.put(ch);

inputFile.close();

outputFile.close();

cout << "File content encrypted and saved to 'encrypted.txt'" << endl;

int main() {
clrscr();

char filename[100];

cout << "Enter the file name to encrypt: ";

cin.getline(filename, sizeof(filename));

encryptFileContent(filename);

getch();

return 0;

OUTPUT:

Write a C++ program to merge two files.

#include <iostream.h>
#include <fstream.h>

#include <conio.h>

void mergeFiles(const char* file1, const char* file2, const char* outputFile) {

ifstream inputFile1(file1);

ifstream inputFile2(file2);

ofstream outputFileStream(outputFile);

if (!inputFile1 || !inputFile2 || !outputFileStream) {

cout << "Error opening files." << endl;

return;

char ch;

// Copy the contents of the first file to the output file

while (inputFile1.get(ch)) {

outputFileStream.put(ch);

// Copy the contents of the second file to the output file

while (inputFile2.get(ch)) {

outputFileStream.put(ch);

}
inputFile1.close();

inputFile2.close();

outputFileStream.close();

cout << "Files merged successfully. Merged content is saved in '" << outputFile << "'"
<< endl;

int main() {

clrscr();

char file1[100];

char file2[100];

char outputFile[100];

cout << "Enter the first file name: ";

cin.getline(file1, sizeof(file1));

cout << "Enter the second file name: ";

cin.getline(file2, sizeof(file2));

cout << "Enter the output file name: ";

cin.getline(outputFile, sizeof(outputFile));
mergeFiles(file1, file2, outputFile);

getch();

return 0;

OUTPUT:

Write a C++ program to Read and Display File’s Content.

#include <iostream.h>

#include <fstream.h>

#include <conio.h>

void displayFileContents(const char* filename) {

ifstream file(filename);
if (!file) {

cout << "Unable to open the file: " << filename << endl;

return;

char ch;

while (file.get(ch)) {

cout << ch;

file.close();

int main() {

clrscr();

char filename[100];

cout << "Enter the file name: ";

cin.getline(filename, sizeof(filename));

displayFileContents(filename);
getch();

return 0;

OUTPUT:

Write a C++ program to List Files in Current Directory.

#include <iostream.h>

#include <conio.h>

#include <dir.h>

int main() {

clrscr();

struct ffblk ffblk;


// Find the first file in the current directory

int result = findfirst("*.*", &ffblk, 0);

if (result == 0) {

do {

cout << ffblk.ff_name << endl;

} while (findnext(&ffblk) == 0);

getch();

return 0;

OUTPUT:

You might also like