0% found this document useful (0 votes)
54 views

CPP SLIP

The document contains multiple C++ programming tasks, including programs for checking maximum and minimum of integers, file handling with classes, calculating volumes of geometric shapes, and employee management with inheritance. Each task includes code snippets demonstrating the implementation of specific functionalities such as operator overloading, function overloading, and command line arguments. The document also features examples of using classes, member functions, and file I/O operations.

Uploaded by

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

CPP SLIP

The document contains multiple C++ programming tasks, including programs for checking maximum and minimum of integers, file handling with classes, calculating volumes of geometric shapes, and employee management with inheritance. Each task includes code snippets demonstrating the implementation of specific functionalities such as operator overloading, function overloading, and command line arguments. The document also features examples of using classes, member functions, and file I/O operations.

Uploaded by

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

Q1a] ) Write a C++ program to check maximum and minimum of two integer numbers.

(Use Inline function and


Conditional operator)

Ans:

#include <iostream>

using namespace std;

inline int max(int a,int b)

if(a>b)

return a;

else

return b;

int main()

std::cout<<"Check maximum and minimum of two integer numbers\n";

cout<<"Enter the value of A\n";

int a;

cin>>a;

cout<<"Enter the value of B\n";

int b;

cin>>b;

cout<<"Maximum Number is :"<<max(a,b);

cout<<"\nMinimum Number is :"<<min(a,b);

}
Q1b] ) Create a C++ class MyFile with data members file pointer and filename. Write necessary member functions to
accept and display File. Overload the following operators:

Operator Example Purpose

+ F3=F1+F2 Concatenate the contents of file FI and F2 in F3.

! !F3 Changes the case of alternate characters of file F3.

//#include<conio.h>

#include<stdio.h>

#include<iostream>

#include<fstream>

#include<stdlib.h>

#include<ctype.h>

#include <string.h>

#define MAXSIZE (10)

using namespace std;

class myfile

FILE * fp;

char fn [ MAXSIZE];

public:

myfile(char * fname)

strcpy(fn,fname);

myfile operator +(myfile);

void operator !();

void display();

};

void myfile::display()

fp-fopen(fn,"r");
char ch;

while((ch-fgetc(fp))!=EOF)

cout<<ch;

fclose(fp);

void myfile::operator ! ()

myfile f4("sy.txt");

char ch;

fp=fopen(fn,"r");

f4.fp-fopen(f4.fn,"w");

while((ch-fgetc(fp))!=EOF)

if(isupper(ch))

fputc(tolower(ch),f4.fp);

else if (islower(ch))

fputc(toupper(ch),f4.fp);

else

fputc(ch,f4.fp);

fclose(fp);

fclose(f4.fp);

remove("abc.txt");

rename("sy.txt", "abc.txt");

myfilemyfile::operator +(myfile f2)


{

myfile f3("abc.txt");

fp-fopen(fn,"r");

f2.fp-fopen(f2.fn, "r");

f3.fp-fopen(f3.fn,"w");

char ch;

while((ch-fgetc(fp))!=EOF)

fputc(ch,f3.fp);

fclose(fp);

while((ch=fgetc(f2.fp))!=EOF)

fputc(ch,f3.fp);

fcloseall();

return f3;

int main()

myfilefl("xyz.txt");

myfile f2("lmn.txt");

myfile f3("abc.txt");

//clrscr();
cout<<" first file \n";

fl.display();

cout<<" second file \n";

f2.display();

f3 =f1+f2;

cout<<" \n After concatnation file is";

f3.display();

cout<<"after changes case \n";

!f3;

f3.display();

//getch();

return 0;

OUTPUT
Q2a]) Write a C++ program to find volume of cylinder, cone and sphere. (Use function overloading).

Ans:

#include<iostream>

#include<conio.h>

using namespace std;

float vol(int,int);//cylinder

float vol(float,int);//cone

float vol(float);//sphere

int main()

int radius, radius2, height, height2;

float radius1;

cout<<"Find the Volume ";

cout<<"\nEnter radius of a cylinder: ";

cin>>radius;

cout<<"\nEnter height of a cylinder: ";

cin>>height;

cout<<"\nVolume of cylinder is: "<<vol(radius,height);

cout<<"\n\nEnter radius of a cone: ";

cin>>radius2;

cout<<"\nEnter height of a cone: ";

cin>>height2;

cout<<"\nVolume of cone is: "<<vol(radius2,height2);

cout<<"\n\nEnter radius of a sphere: ";

cin>>radius1;

cout<<"\nVolume of sphere is: "<<vol(radius1);


getch();

return 0;

float vol(int radius,int height)

return(3.14*radius*radius*height);

float vol(float radius2, int height2)

return(0.33*3.14*radius2*radius2*height2);

float vol(float radius1)

return((4*3.14*radius1*radius1*radius1)/3);

Output:-
Q2b] Write a C++ program to create a class Movie with data members M_Name, Release_Year, Director_Name and
Budget. (Use File Handling) Write necessary member functions to:

i. Accept details for 'n' Movies from user and store it in a file "Movie.txt".

ii.details from a file.

iii. Count the number of objects stored in a file.

#include<iostream>

#include<conio.h>

#include<fstream>

using namespace std;

class Movie

public:

int year;

char mname[20];

char dname[15];

int budget;

public:

void accept()

cout<<"\n Enter the movie name:-";

cin>>mname;

cout<<"\n Enter the Release year:-";

cin>>year;

cout<<"\n Enter the Director Name:-";

cin>>dname;

cout<<"\n Enter the Budget-";

cin>> budget;

void display()

cout<<"\n\n The movie name- "<<mname;

cout<<"\n The Release year- "<<year;

cout<<"\n The Director Name- "<<dname;


cout<<"\n The Budget-"<<budget;

};

int main()

Movie m[5];

int n,i;

fstream file;

file.open("Movie.bet", ios::in|ios::out);

cout<<"\nEnter the number of records you want to enter-";

cin>>n;

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

m[i].accept();

file.write((char*)&m[i], sizeof(m[i]));

cout<<"\n\nDetails of Movie from the file-: ";

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

file.read((char*) &m[i],sizeof(m[i]));

m[i].display();

file.close();

getch();

OUTPUT
Q3a]Write a C++ program to interchange values of two integer numbers. (Use call by reference)

#include<iostream>

#include<conio.h>

using namespace std;

class integer

public:

int x,y;

void getdata();

void display();

void(swap(int&,int&));

};

void integer::getdata()

cout<<"\nEnter value of x:-";

cin>>x;

cout<<"\nEnter value of y:-";

cin>>y;

void integer::display()

cout<<"\nDisplayvalues:-";

cout<<"x="<<x;

cout<<"\ny="<<y;

void integer::swap(int&p,int&q)

int temp;

temp=p;

p=q;

q=temp;

cout<<"\n\nAfterswaping\n\n";
cout<<"\nx="<<y;

cout<<"\ny="<<x;

int main()

int x,y;

integer i;

i.getdata();

i.display();

i.swap(x,y);

getch();

OUTPUT
Q3b]) Write a C++ program to accept 'n' numbers from user through Command Line Argument.

Store all Even and Odd numbers in file "Even.txt" and "Odd.txt" respectively.

#include<iostream>

#include<fstream>

#include<stdlib.h>

#define MAX 10

using namespace std;

int main()

int num[MAX],n;

cout<<"Enter the Limit\n";

cin>>n;

cout<<"Enter the elements\n";

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

cin>>num[i];

ofstream fout1,fout2;

fout1.open("Even.txt");

if(fout1.fail())

cout<<"Couldn't open file:\n Odd.txt";

exit(1);

fout2.open("Odd.txt");

if(fout2.fail())

cout<<"Couldn't open file:\n Even.txt";

exit(1);

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

if(num[i]%2==0)

fout1<<num[i]<<"";
else

fout2<<num[i]<<"";

fout1.close();

fout2.close();

ifstream fin1, fin2;

fin1.open("Odd.txt");

if(fin1.fail())

cout<<"Couldn't open file:\n Odd.txt";

exit(1);

fin2.open("Even.txt");

if(fin2.fail())

cout<<"Couldn't open file:\n Even.txt";

exit(1);

cout<<"Contents of Odd.txt\n";

do

char ch;

fin1.get(ch);

cout<<ch<<"";

while(fin1);

fin1.close();

cout<<"\nContents of Even.txt\n";

do

char ch;

fin2.get(ch);

cout<<ch<<"";

}
while(fin2);

fin2.close();

cout<<"\n";

system("pause");

return 0;

OUTPUT
Q4a]A C++ program to accept Worker information Worker Name, No_ofHours_worked, Pay_Rate and Salary.
Write necessary functions to calculate and display the salary of Worker. (Use default value for Pay_Rate)

#include<iostream>

#include<conio.h>

using namespace std;

class worker

public:

char worker_name[20];

int salary,hrworked, pay_rate;

void calsalary(int pay_rate=25)

cout<<"\n Worker information ";

cout<<"\n________________________________";

cout<<"\n\nEnter the name of worker: ";

cin>>worker_name;

cout<<"Enter no of hours worked: ";

cin>>hrworked;

salary=pay_rate*hrworked;

cout<<"\n________________________________";

cout<<"\n\nSalary of worker: Rs."<<salary;

cout<<"\n________________________________";

};

int main()

worker w;

w.calsalary();

getch();

OUTPUT
Q4b]Write a C++ program to create a base class Employee (Emp-code, name, salary). Derive two classes as
Fulltime (daily_wages, number_of_days) and Parttime(number_of working hours, hourly wages).

Write a menu driven program to perform following functions:

1. Accept the details of 'n' employees and calculate the salary

2. Display the details of 'n' employees.

3. Display the details of employee having maximum salary for both types of employees

#include<iostream>

using namespace std;

const int m=50;

class emp

public:

int empno;

char empname[30];

public:

void get()

cout<<"\n Enter Employee No. :";

cin>>empno;

cout<<"\n Enter Employee Name :";

cin>>empname;

};

class fulltime:public emp

public:

float daily_rate;

int days;

int salary;

public:

void getdata()

{
cout<<"\n Enter Daily Rate :";

cin>>daily_rate;

cout<<"\n Enter No. of Days :";

cin>>days;

void cal()

salary=daily_rate*days;

cout<<"\n Salary :"<<salary;

void show()

cout<<"\n ----------------------------------\n";

cout<<"\n Employee Number :"<<empno;

cout<<"\n Employee Name :"<<empname;

cout<<"\n Salary :"<<salary;

cout<<"\n Status : Fulltime";

cout<<"\n ----------------------------------\n";

};

class parttime:public emp

public:

int hourly_rate;

int working_hours;

int salary1;

public:

void get1()

cout<<"\n Enter Hourly Rate :";

cin>>hourly_rate;

cout<<"\n Enter Working Hours :";

cin>>working_hours;

}
void cal1()

salary1=hourly_rate*working_hours;

cout<<"\n Salary :"<<salary1<<endl;

void show1()

cout<<"\n ----------------------------------\n";

cout<<"\n Employee No :"<<empno;

cout<<"\n Employee Name :"<<empname;

cout<<"\n Salary :"<<salary1;

cout<<"\n Status : Part time";

cout<<"\n ----------------------------------\n";

};

int main()

int constcnt=5;

int var=0;

int var1=0;

fulltime f1[cnt];

parttime p1[cnt];

int x,i;

do

cout<<"\n";

cout<<"\n 1.Enter Record";

cout<<"\n 2.Display Record";

cout<<"\n 3.Search Record";

cout<<"\n 4.Quit";

cout<<"\n\n Enter Your Choice :";

cin>>x;
switch(x)

case 1:

int y;

cout<<"\n 1. Fulltime Employee";

cout<<"\n 2. Parttime Employee \n";

cout<<"\n Enter :";

cin>>y;

switch(y)

case 1:

f1[var].get();

f1[var].getdata();

f1[var].cal();

var++;

break;

case 2:

p1[var1].get();

p1[var1].get1();

p1[var1].cal1();

var1++;

break;

break;

case 2:

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

f1[i].show();

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

p1[i].show1();

break;
case 3:

int a;

cout<<"\n Enter Employee No. :";

cin>>a;

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

if (f1[i].empno==a)

f1[i].show();

if(p1[i].empno==a)

p1[i].show1();

} while(x!=4);

return 0;

OUTPUT
Q5a]a) Consider the following C++ class class Point int x,y; public: void setpoint(int, int);// To set the values of x
and y co-ordinate. void showpoint();// To display co-ordinate of a point P in format (x, y).

#include<iostream>

using namespace std;

class point

int x,y;

public:

void setpoint(int p, int q)

x=p;

y=q;

void showpoint()

cout<<"x "<<x<<endl<<"y "<<y;

};

int main()

point a;

a.setpoint(9,19);

a.showpoint();

return(0);

OUTPUT
Q5b]

Create a C++ base class Shape. Derive three different classes Circle, Sphere and Cylinder from shape class. Write a
C++ program to calculate area of Circle, Sphere and Cylinder. (Use pure virtual function).

#include<iostream>

#include<conio.h>

#include<stdlib.h>

using namespace std;

class shape

public:

virtual void area()=0;

};

class Circle:public shape

public:

int r;

void getc()

cout<<"\n Enter the radius: ";

cin>>r;

void area();

};

class Sphere:public shape

public:

int r1;

void getr()

cout<<"\n Enter the radius1: ";

cin>>r1;

void area();
};

class Cylinder:public shape

public:

int r2,h;

void gett()

cout<<"\n Enter the radius2: ";

cin>>r2;

cout<<"\n Enter the height: ";

cin>>h;

void area();

};

void Circle::area()

cout<<(3.14*r*r);

void Sphere::area()

cout<<(4*3.14*r1*r1);

void Cylinder::area()

int d=h+r2;

cout<<d;

cout<<(2*3.14*r2*d);

int main()

{
int ch;

Circle c;

Sphere S;

Cylinder t;

do

cout<<"\n\n 1. Area of Circle: ";

cout<<"\n 2. Area of Sphere: ";

cout<<"\n 3. Area of Cylinder: ";

cout<<"\n 4. Exit";

cout<<"\n Enter your Choice: ";

cin>>ch;

switch(ch)

case 1:

c.getc();

cout<<"\n Area of Circle: ";

c.area();

cout<<"\n_____________________________________";

break;

case 2:

S.getr();

cout<<"\n Area of Sphere: ";

S.area();

cout<<"\n_____________________________________";

break;

case 3:

t.gett();

cout<<"\n Area of Cylinder: ";

t.area();

cout<<"\n_____________________________________";
break;

case 4:

exit(0);

}while(ch!=4);

getch();

};

OUTPUT
Q6a]) Write a C++ program to create two Classes Square and Rectangle. Compare area of both the shapes using
friend function. Accept appropriate data members for both the classes

#include<iostream>

#include<conio.h>

using namespace std;

class Square

friend class Rectangle;

int side;

public:

Square(int s)

side=s;

};

class Rectangle

int length;

int breadth;

public:

int getArea()

return length*breadth;

void shape(Square a)

length = a.side;

breadth = a.side;

};

int main()
{

Square square(5);

Rectangle rectangle;

rectangle.shape(square);

cout<<rectangle.getArea();

return 0;

getch();

OUTPUT
Q6b]b) Create a C++ class class Matrix { int * *p int r, c; public: //member functions }

Write necessary member functions to:

i. Accept Matrix elements

ii. Display Matrix elements.

iii. Calculate transpose of a Matrix.

(Use constructor and destructor)

#include<iostream>

using namespace std;

class MATRIX

int a[5][5];

int row,col;

public: MATRIX(int x,int y)

row=x;

col=y;

void read();

void display();

void transpose();

};

void MATRIX::read()

cout<< "Enter elements of matrix:\n";

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

for(int j=0;j<col;j++) cin>>a[i][j];

void MATRIX::display()

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

for(int j=0;j<col;j++)
cout<<a[i][j]<<"";

cout<<"\n";

void MATRIX::transpose()

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

for(int j=0;j<col;j++)

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

cout<<"\n";

int main()

int m,n;

cout<<"Enter order of matrix:"; cin>>m>>n;

MATRIX obj1(m,n);

obj1.read();

cout<<"Matrix is…\n";

obj1.display();

cout<<"Transpose of matrix is…\n";

obj1.transpose();

OUTPUT
Q7a]Write a C++ program using class with data member char str[50] and function replace (char chl, char ch2) every
occurrence of chl in str should be replaced with ch2 and return number of replacement it makes use default value
for char ch2. (Use ch2 as Default Argument)

#include<conio.h>

#include<iostream>

#include<string.h>

class mystr

public:int replace(char *str,char,char);

};

int mystr::replace(char *str,char() c2='r');

while(str!='\0')

if(* str==c1)

*str==c2;

n++;

str=str-i;

return n;

int main()

mystr m;

char *str,c1,c2;

cout<<"enter string";

cin>>str;

cout<<"enter character with replace";

cin>>a;

cout<<"number of replacement="<<m.replace(str,c1,c2);

cout<<"after replacement atring is="<<str;


getch();

return 0;

OUTPUT
Q7b]b) Create a C+ + class Vector with data members size & pointer to integer. The size of the vector varies so

the memory should be allocated dynamically. Perform following operations: Accept a vector Display a vector in the
format (10, 20, 30,....) iii. Calculate union of two vectors. (use parameterized constructor & copy

#include<iostream>

using namespace std;

class vector

int *a ,*b;

int n ,n1;

public:

int create()

int i,j;

cout<<"\nEnter the dimensions of the vector space:";

cin>>n;

a=new int[n];

cout<<"\nEnter first the vector :";

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

cin>>a[i];

cout<<"\nEnter the dimension of the vector space:";

cin>>n1;

b=new int[n1];

cout<<"\nEnter second the vector :";

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

cin>>b[j];
}

int display()

int i,j;

cout<<"\nThe first vector is: (";

for(i=0;i<n-1;i++);

cout<<a[i]<<",";

cout<<a[n-1]<<")";

cout<<"\nThe second vector is: (";

for(j=0;j<n1-1;j++);

cout<<b[j]<<",";

cout<<b[n1-1]<<")";

};

int main()

vector v;

int ch;

do{

cout<<"\n1.Accept vector\n2.Display vector\nUnion";

cout<<"\nEnter Choice:";

cin>>ch;

switch(ch)

case 1:

v.create();

break;

case 2:

v.display();
break;

case 3:

vector<int>v(10);

vector<int>::iterator st;

sort(a[i],a[i]+3);

sort(b[j],b[j]+3);

st=set_union(a[i],a[i]+3,b[j],b[j]+3,v.begin());

v.resize(st-v.begin());

cout<<"The union between the sets has "<<(v.size())<<"elements:"<<endl;

for(st=v.begin();st!=v.end();++st)

cout<<*st<<"";

break;

}while(ch!=3);

OUTPUT
8a) Write a C++ program to create a class Number, which contain static data member 'ent' and member function
'Display()'. Display() should print number of times display operation is performed irrespective of the object
responsible for calling Display() .

#include<iostream>

#include<conio.h>

using namespace std;

class Number

static int cnt;

public:

static void display()

cout<<"\n Number of time call show: "<<cnt;

cnt++;

};

int Number::cnt;

int main()

Number s1,s2,s3,s4;

s1.display();

s2.display();

s3.display();

s4.display();

getch();

OUTPUT
8b) Create a C++ class Person with data members Person_name, Mobile number, Age, City. Write necessary
member functions for the following:

i. Search the mobile number of given person.

ii. Search the person name of given mobile number.

iii. Search all person details of given city.

(Use function overloading

long phoneno;

public:

void get_data()

cout<<"\n Enter the Name:";

cin>>name;

cout<<"\n Enter the phone number:";

cin>>phoneno;

cout<<"\n Enter the city:";

cin>>city;

void display_data()

cout<<"\n Name: "<<name<<"\n Phone no:"<<phoneno<<"\nCity: "<<city;

void searchname(char x[])

if(strcmp(name,x)==0)

cout<<"\n Name: "<<name<<"\n Phone no:"<<phoneno;

void searchpno(int x)

if((phoneno==x)==1)

cout<<"\n name:"<<name<<"\n Phone no:"<<phoneno;


}

void searchcity(char c[])

if(strcmp(city,c)==0)

display_data();

};

int main()

Person t[30],n[20],c[20];

int num,ch,i,pno;

char cont,cty[30],name1[30];

cout<<"\n 1.Accept data";

cout<<"\n 2.Search by name";

cout<<"\n 3.Search by phoneno";

cout<<"\n 4.Search by city";

cout<<"\n 5. display customer:\n";

do{

cout<<"\n Enter your choice: ";

cin>>ch;

switch(ch)

case 1:cout<<"\n How many Customer you want to enter: ";

cin>>num;

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

t[i].get_data();

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

{
t[i].display_data();

break;

case 2:

cout<<"\n Enter name to search phoneno: ";

cin>>name1;

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

t[i].searchname(name1);

break;

case 3:

cout<<"\n Enter telephoneno to search name: ";

cin>>pno;

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

t[i].searchpno(pno);

break;

case 4:

cout<<"\n Enter city name";

cin>>cty;

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

t[i].searchcity(cty);

break;

cout<<"\n Do you want to continue:";

cin>>cont;

}while(cont=='Y'||cont=='y');

}}}

OUTPUT
9a) Consider the following C++ class class Person char Name [20]; charAddr [30] float Salary; float tax_amount;
public: // member functions

Calculate tax amount by checking salary of a person

For salary <=20000 tax rate=0

For salary >20000|<=40000 tax rate= 5% of salary.

For salary >40000 tax rate =10% of salary.

#include<iostream>

#include<conio.h>

using namespace std;

class person

char name[20];

char addr[20];

float sal,tax;

public:

void get()

cout<<"\n Enter the name: ";

cin>>name;

cout<<"\n Enter the address: ";

cin>>addr;

cout<<"\n Enter the salary: ";

cin>>sal;

void put()

cout<<"_____________________________________\n";

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

cout<<"_____________________________________\n";

cout<<"Name:\tAddress:\tSalary:\tTax:\n";

cout<<name<<"\t"<<addr<<"\t"<<sal<<"\t"<<tax<<endl;

void cal_tax()
{

if(sal<=20000)

tax=0;

else if(sal>=20000||sal<=40000)

tax=(sal*5)/100;

else if(sal>40000)

tax=(sal*10)/100;

};

int main()

person p;

p.get();

p.cal_tax();

p.put();

getch();

OUTPUT
9b) Create a C++ class Time with data members Hours, Minutes and Seconds. Write necessary member functions
for the following: (Use Objects as arguments)

i. To accept a time.

i. To display a time in format hh:mmiss.

iii. To find difference between two time and display it in format hh:mm:ss.
10a) Write a C++ program to create a class Account with data members Acc_number, Acc_type and Balance. Write
member functions to accept and display 'n' account details. (Use dynamic memory allocation

#include<iostream>

#include<conio.h>

#include<stdlib.h>

using namespace std;

class Account

public:

int Acc_no,Balance;

char Acc_type[30];

public:

Account() { cout<<"Constructor"<<endl;}

~Account() { cout<<"Destructor"<<endl;}

void get_data()

cout<<"\n Enter Acc_no.:";

cin>>Acc_no;

cout<<"\n Enter Acc_type:";

cin>>Acc_type;

cout<<"\n Enter Balance:";

cin>>Balance;

void display_data()

cout<<"\n Account details:\n";

cout<<"\t"<<Acc_no<<"\t"<<"\t"<<Acc_type<<"\t"<<Balance;

};

int main()

{
int i,num;

Account*a=new Account[4];

delete[]a;

cout<<"\n How many records u want?:";

cin>>num;

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

a[i].get_data();

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

a[i].display_data();

return 0;

OUTPUT
Q10b]b) Create a C++ class City with data members City_code, City_name, population. Write necessary member

functions for the following:

i. Accept details of n cities

ii. Display details of n cities in ascending order of population.

iii. Display details of a particular city.

Use Array of object and to display city information use manipulators.)


11a]a) Create a C++ class MyArray, which contains single dimensional integer array of a given size. Write a
member function to display sum of given array elements. (Use Dynamic Constructor and Destructor

#include<stdio.h>

#include<conio.h>

#include<iostream>

using namespace std;

class MyArray

int size,*ptr,*p;

int sum=0;

public:

MyArray(int no)

size=no;

ptr=new int[size];

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

cout<<"enter elements";

cin>>ptr[i];

sum = sum+ptr[i];

void display()

cout<<"elements are";

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

cout<<ptr[i]<<"\t";

cout<<"\n\nSum of all elements is: "<<sum;

}
void sum()

int arr[10];

int i;

int sum=0;

cout<<"Enter 10 array elements: ";

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

cin>>arr[i];

sum = sum + arr[i];

cout<<"\nThe array elements are: \n";

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

cout<<arr[i]<<"";

cout<<"\n\nSum of all elements is: "<<sum;

MyArray()

delete ptr;

};

void main()

int n;

cout<<"enter size";

cin>>n;

MyArray d(n);

d.display();

d.sum();

getch();

}
OUTPUT
Q11b]b) Create a base class Student with data members Roll No, Name. Derives two classes from it,

Theory with data members MI, M2, M3, M4 and class Practical with data members PI, P2. Class

Result(Total_Marks, Percentage, Grade) inherits both Theory and Practical classes. (Use concept of

Virtual Base Class and protected access specifiers)

Write a C++ menu driven program to perform the following functions:

Accept Student Information

ii. Display Student Information

iii. Calculate Total_marks, Percentage and Grade.

#include<conio.h>

#include<string.h>

#include<conio.h>

#include<iostream>

using namespace std;

class student

protected:

int rno;

char name[20];

public:

void getdetails();

};

class Theory:public virtual student

protected:

int mark1, mark2,mark3,mark4;

public:

void getmarks();

};

class Practical : virtual public student

protected:

int p1,p2;

public:
void getpractical();

};

class result :public Theory, public Practical

int total_marks;

float per;

char grade [10];

public:

void calc();

void sort(result& ,result&);

void display();

};

void student::getdetails()

cout<<"\n enter roll no and name :";

cin>>rno>>name;

void Theory::getmarks()

cout<<"\n enter marks of four subject :";

cin>>mark1>>mark2>>mark3>>mark4;

void Practical::getpractical()

cout<<"\n Enter Practical Details :";

cin>>p1>>p2;

void result::calc()

int i;

total_marks=mark1+mark2+mark3+mark4+p1+p2;

per=total_marks/(float)6;
if(per<50)

strcpy(grade,"C");

else

if(per<60)

strcpy(grade,"B");

else

if( per<75)

strcpy(grade,"A");

else

strcpy(grade,"A+");

cout<<"\n calculation complete\n";

void result::sort(result &r1,result &r2)

result rt;

if(r1.total_marks>r2.total_marks)

rt=r1;

r1=r2;

r2=rt;

void result::display()

cout<<"\n roll no="<<rno<<"\n name ="<<name;

cout<<"\n mark1="<<mark1<<"\n mark2 ="<<mark2<<"\n mark3="<<mark3<<"\n mark4="<<mark4;

cout<<"\n Practical P1="<<p1<<"\n Practical P2="<<p2<<"\n percentage ="<<per<<"\n grade ="<<grade;

int main()

int n,i,ch,j;

result r[20];

do
{

cout<<"\n MENU \n";

cout<<"\n 1.build master table \n 2. calculate total & grade \n";

cout<<"\n 3.display result in asscending order \n enter your choice : ";

cin>>ch;

switch(ch)

case 1:

cout<<"In how many student :";

cin>>n;

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

cout<<"enter student detailse \n";

r[i].getdetails();

r[i].getmarks();

r[i].getpractical();

break;

case 2:

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

r[i].calc();

break;

case 3:

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

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

r[i].sort(r[i],r[j]);

r[i].display();

break;

} while(ch<=3);

getch();

return 0;
}

OUTPUT
Q12a]a) Write a C++ program to create a class Date with data members day, month, and year. Use default and

parameterized constructor to initialize date and display date in dd-Mon-yyyy format. (Example: Input:

04-01-2021 Output: 04-Jan-2021)

#include<conio.h>

#include<iostream>

class date

int dd,mm,yyyy;

public:

date(int d,int m,int y) // creating parameterized constructor

dd=d;

mm=m;

yyyy=y;

void disp()

if(mm>12)

cout<<"Invalid Month."; //mm is month if it is greater than 12 then it is invalid.

else

cout<<"Input :"<<dd<<"/"<<mm<<"/"<<yyyy<<endl;

if(mm==1)

cout<<"Output :"<<dd<<"/"<<"jan"<<"/"<<yyyy;

else if(mm==2)

cout<<"Output :"<<dd<<"/"<<"Feb"<<"/"<<yyyy;

else if(mm==3)

{
cout<<"Output :"<<dd<<"/"<<"mar"<<"/"<<yyyy;

else if(mm==4)

cout<<"Output :"<<dd<<"/"<<"apr"<<"/"<<yyyy;

else if(mm==5)

cout<<"Output :"<<dd<<"/"<<"may"<<"/"<<yyyy;

else if(mm==6)

cout<<"Output :"<<dd<<"/"<<"jun"<<"/"<<yyyy;

else if(mm==7)

cout<<"Output :"<<dd<<"/"<<"july"<<"/"<<yyyy;

else if(mm==8)

cout<<"Output :"<<dd<<"/"<<"Aug"<<"/"<<yyyy;

else if(mm==9)

cout<<"Output :"<<dd<<"/"<<"sep"<<"/"<<yyyy;

else if(mm==10)

cout<<"Output :"<<dd<<"/"<<"oct"<<"/"<<yyyy;

else if(mm==11)

cout<<"Output :"<<dd<<"/"<<"Nov"<<"/"<<yyyy;
}

else if(mm==12)

cout<<"Output :"<<dd<<"/"<<"Dec"<<"/"<<yyyy;

};

int main()

int d,m,y;

cout<<"Enter a date :";

cin>>d;

cout<<"Enter a month: ";

cin>>m;

cout<<"Enter a year :";

cin>>y;

date d1(d,m,y); //creating instance of class date

d1.disp();

getch();

return(0);

OUTPUT
Q12B) write a c++ class wait and data members kilogram gram write a c++ program using operator overloading to
perform following function

i) To accept weight

ii) to display weight kilogram and gram

iii) overload += operator to add to weight

iv) overload += to operator check equality of two weight

#include<iostream>

#include<conio.h>

using namespace std;

class Weight

int kilogram,gram;

public:

void getdata()

cout<<"\n\nEnter the kilogram:\t";

cin>>kilogram;

cout<<"\nEnter the gram:\t";

cin>>gram;

void display()

cout<<"\nAddition of two weights:\t";

cout<<kilogram<<"."<<gram;

void display2()

cout<<kilogram<<"."<<gram;

Weight operator+=(Weight &d)

Weight t;

t.kilogram=d.kilogram+kilogram;
t.gram=d.gram+gram;

return t;

int operator==(Weight &d)

if(kilogram==d.kilogram || gram==d.gram)

return 1;

else

return 0;

};

int main()

Weight c1,c2,c3,c4,c5

c1.getdata();

C2.getdata();

c3=c1+=c2;

c3.display();

c4.getdata();

c5.getdata();

if(c4==c5)

cout<<"\n";

c4.display2();

c5.display2();

cout<<"\t Both are same\t";


}

else

cout<<"\n";

c5.display2();

c4.display2();

Cout<<"\t Both are not same\t";

getch();

OUTPUT
13a]Write a C++ program to create a class Product with data members Product id, Product_Name, Qty,

Price. Write member functions to accept and display Product information also display number of objects

created for Product class. (Use Static data member and Static member function

#include<iostream>

using namespace std;

class Item

int code,price;

public:

void accept()

cout<<"\n\n Enter code and price: ";

cin>>code>>price;

void display(Item a[])

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

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

cout<<"\n Code: "<<a[i].code;

cout<<"\n Price: "<<a[i].price;

void sum(Item a[])

int s=0;

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

s=a[i].price+s;

cout<<"\n\n Sum of all items: "<<s;

};
int main()

Item e[5];

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

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

e[i].accept();

e[1].display(e);

e[1].sum(e);

return 0;

OUTPUT
13b) Create a C++ class Cuboid with data members length, breadth, and height. Write necessary member functions
for the following:

i. void setvalues(float, float,float) to set values of data members.

ii. void getvalues( ) to display values of data members.

iii. float volume( ) to calculate and return the volume of cuboid.

iv. float surface_area( ) to calculate and return the surface area of cuboid.

(Use Inline function)

#include<stdio.h>

#include<conio.h>

#include<iostream>

using namespace std;

class Cuboid

float a,b,c;

public:

void getdata()

cout<<"\n Enter the value of a :";

cin>>a;

cout<<"\n Enter the value of b :";

cin>>b;

cout<<"\n Enter the value of c :";

cin>>c;

void setdata()

cout<<a<<b<<c;

inline void volume()

cout<<"\n The volume of cuboid =\t"<<a*b*c;

inline void surface_area()

{
cout<<"\n The surface area of cuboid =\t"<<2*a*b+2*a*c+2*b*c;

};

int main()

Cuboid c;

c.getdata();

c.volume();

c.surface_area();

getch();

OUTPUT
14a]) Write a C++ program to accept radius of a Circle. Calculate and display diameter, circumference as well as
area of a Circle. (Use Inline function)

#include <iostream>

using namespace std;

inline float dimeter(float r)

return (r*2);

inline float circlearea(float r){

return(3.14*r*r);

inline float circumference (float r){

return(3.14*2*r);

int main()

float radius;

cout<<"\nEnter the radius of the circle:";

cin>>radius;

cout<<"\nDiameter of Circle:"<<dimeter(radius);

cout<<"\nArea of Circle:"<<circlearea(radius);

cout<<"\nCircumference of Circle:"<<circumference(radius);

return 0;

OUTPUT
Q14b]) Create a C++ class MyString with data members a character pointer and str_length. (Use new and

delete operator). Write a C++ program using operator overloading to perform following operation:

To reverse the case of each alphabet from a given string 11. < To compare length of two strings.+

To add constant 'n' to each alphabet of a string.

#include<string.h>

#include<iostream>

#include<conio.h>

class mystring

public:

int n;

int i,c;

char *p; //p=string

void accept()

cout<<"Enter how many lenght of string: ";

cin>>n;

p=new char[n+1]; // allocate memory dynamically

cout<<"Enter string: ";

cin>>p;

void disp()

cout<<"\nstring is: "<<p;

void operator !()

c=strlen(p); // strlen() is function is used to lenght of string

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

if(p[i]>64&&p[i]<91)

p[i]=p[i]+32;
}

else

p[i]=p[i]-32;

cout<<"\nChange string is: "<<p;

void operator [](int l)

int e;

char m;

e=l;

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

if(e-1==i)

cout<<"Character is: "<<p[i];

};

int main()

int e,ch; // e=index

do{

cout<<"\n\n1.Accept string\n2.Display string\n3.Change the case of each alphabet\n4.print a character present at


specified index\n5.Exit\nEnter your choice :- ";

cin>>ch;

switch(ch)

case 1: mystring m;

m.accept();break;
case 2: m.disp();break;

case 3: !m;break;

case 4: cout<<"\nEnter index: ";

cin>>e;

m[e];break;

}while(ch!=5);

OUTPUT
Q15a]Create a C++ class Fraction with data members Numerator and Denominator. Write a C++ program to
calculate and display sum of two fractions. (Use Constructor)

#include<iostream>

#include<conio.h>

using namespace std;

class fraction

int num;

int deno;

public:

fraction()

fraction(int a,int b)

num=a;

deno=b;

void display()

cout<<"\n The numerator is:"<<num;

cout<<"\n The denominator is:"<<deno;

cout<<"\nFraction is:"<<num<<"/"<<deno;

friend fraction operator +(fraction&,fraction&);

};

fraction operator +(fraction&p,fraction&q)

fraction a;

a.num=p.num + q.num;

a.deno=p.deno + p.deno;

return a;

}
int main()

fraction f1(2,1);

fraction f2(3,2);

fraction f3;

f1.display();

f2.display();

f3=f1+f2;

cout<<"\n After Addition: ";

f3.display();

getch();

OUTPUT
Q15b]Write a C++ class Seller (S_Name, Product name, Sales_Quantity, Target_Quantity, Month,Commission).
Each salesman deals with a separate product and is assigned a target for a month. At the end of the month his
monthly sales is compared with target and commission is calculated as follows: If Sales_Quantity>Target_Quantity
then commission is 25% of extra sales made + 10% of target. If Sales_Quantity = Target_Quantity then commission
is 10% of target. Otherwise commission is zero.

Display salesman information along with commission obtained. (Use array of objects)

#include<iostream>

#include<conio.h>

using namespace std;

class sales

char sname[20],pname[20];

int squantity,target,month;

float commission;

public:

void get()

cout<<"Enter salesman name:\t";

cin>>sname;

cout<<"\nEnter product name:\t";

cin>>pname;

cout<<"\nEnter sales quantity:\t";

cin>>squantity;

cout<<"\nEnter target:\t";

cin>>target;

cout<<"\nEnter month:\t";

cin>>month;

void

put()

cout<<"\nSalesman name:\t"<<sname;

cout<<"\nProduct name:\t"<<pname;

cout<<"\nSales quantity:\t"<<squantity;

cout<<"\nTarget:\t"<<target;
cout<<"\nMonth:\t"<<month;

commission=0;

if(squantity>target)

commission=commission+((squantity-target)*0.25)+(target*0.10);

else

if(target=squantity)

commission=commission+(target*0.10);

else

commission=0;

cout<<"\nCommission:\t"<<commission;

};

int main()

sales sman[10];

int i,n;

cout<<"\nEnter how many Salesman : ";

cin>>n;

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

sman[i].get();

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

sman[i].put();

getch();

return 0;
}

OUTPUT

You might also like