You are on page 1of 15

Q. Define a class Tour in C++ with the description given below.

Private members:
Tcode of type string
Noofadults of type integer
Noofkids of type integer
Kilometeres of type integers
Totalfare Of type float

Public members:
A constructor to assign initial values as follows:
Tcode with the word NULL.
Assign all other with zero.
A function Assignfare() which calculates and assigns the value of the data
members total fare as follows:
For each Adult
Fare(Rs) For Kilometers
500 >=1000
300 <1000&>=500
200 <500
For kids the below price will be 50%
A function entertour() and showtour().

Ans-

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

class tour
{
private:
char tcode[5];
int noa,nok,kilo;
float total;
public:
tour()
{
tcode[5]=NULL;
noa=nok=total=kilo=0;
}
void assignfare()
{
total=0;
for(int i=0;i<noa;i++)
{
if(kilo>=1000)
total+=500;
else if(kilo>=5000)
total+=300;
else
total+=200;
}
for(int j=0;j<nok;j++)
{
if(kilo>=1000)
total+=250;
else if(kilo>=5000)
total+=150;
else
total+=100;
}
}
void enter()
{
cout<<"Enter code = ";
cin>>tcode;
cout<<"Enter no of adults = ";
cin>>noa;
cout<<"Enter no of kids = ";
cin>>nok;
cout<<"Enter kilometers =";
cin>>kilo;
assignfare();
}
void show()
{
cout<<"\nCode = "<<tcode<<endl;
cout<<"No of adults = "<<noa<<endl;
cout<<"No of kids = "<<nok<<endl;
cout<<"Kilometers = "<<kilo<<endl;
cout<<"Total Fare = "<<total<<endl;
}
};
void main()
{
clrscr();
tour t;
t.enter();
t.show();
getch();
}

OUTPUT-
Q. Write a program to explain the working of the scope resolution
operator?

Ans-

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

char c;
void main()
{
clrscr();
char c;
cout<<"Enter Global Variable Data = ";
cin>>::c;
cout<<"Enter Local Variable Data = ";
cin>>c;
cout<<"Local Variable="<<c<<endl;
cout<<"Global Variable="<<::c;
getch();
}

OUTPUT-
Q. Write a program to show concept of virtual base class?

Ans-

#include<iostream.h>
#include<conio.h>
class A
{
public:
int x;
};
class B:virtual public A
{
public:
int y;
};
class C:virtual public A
{
public:
int z;
};
class D:public B,public C
{
public:
int sum;
};
void main()
{
clrscr();
D d;
d.x=10;
d.y=20;
d.z=30;
d.sum=d.x+d.y+d.z;
cout<<"x = "<<d.x<<endl;
cout<<"y = "<<d.y<<endl;
cout<<"z = "<<d.z<<endl;
cout<<"Sum = "<<d.sum<<endl;
getch();
}
OUTPUT-
Q. Define a class Applicant in C++ with the following description:
Private Members:
*A data member ANo of type long
*A data member Name of type string
*A data member Agg of type float
*A data member Grade of type char
*A member function Grademe() to find the Grade as per the aggregate
marks obtained by a student. Equivalent Aggregate Marks range and the
respective grades are shown as follows:
Aggregate Marks Grade
>=80 A
Less than 80 and >=65 B
Less than 65 and>=50 C
Less than 50 D
Public Members:
*A function Enter() to allow user to enter values for ANo, Name, Agg
and call function Grademe() to find the grade.
*A function Result() to allow user to view the content of all the data
members.

Ans-

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

class applicant
{
private:
long ano;
char name[50];
float agg;
char grade;
void grademe()
{
if(agg>=80)
grade='A';
else if(80>agg>=65)
grade='B';
else if(65>agg>=50)
grade='C';
else
grade='D';
}
public:
void enter()
{
cout<<"Enter Admission No. = ";
cin>>ano;
cout<<"Enter Name = ";
gets(name);
cout<<"Enter Agg = ";
cin>>agg;
grademe();
}
void result()
{
cout<<"\nANo: "<<ano<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Agg: "<<agg<<endl;
cout<<"Grade: "<<grade<<endl;
}
};
void main()
{
clrscr();
applicant a;
a.enter();
a.result();
getch();
}
OUTPUT-
Q. Write a complete program to find and display the sum of each row
and column of a 2D array of type int, let the user enter the matrix.

Ans-

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

void main()
{
clrscr();
int a[10][10],i,j,r[10],c[10],row,col;
cout<<"Enter no of rows and columns = ";
cin>>row>>col;
cout<<"Enter matrix = ";
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
cin>>a[i][j];
}
for(i=0;i<row;i++)
{
r[i]=0;
for(j=0;j<col;j++)
r[i]=r[i]+a[i][j];
cout<<"Sum of row "<<i+1<<" is = "<<r[i]<<endl;
}
for(j=0;j<col;j++)
{
c[j]=0;
for(i=0;i<row;i++)
c[j]=c[j]+a[i][j];
cout<<"Sum of column "<<j+1<<" is = "<<c[j]<<endl;
}
getch();
}
OUTPUT-
Q. Write a complete program applying selection sort on an unsorted
array, and let user enter the array.

Ans-

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

void main()
{
clrscr();
int i,j,n,loc,temp,min,a[30];
cout<<"Enter number of elements = ";
cin>>n;
cout<<"Enter elements = ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n-1;i++)
{
min=a[i];
loc=i;
for(j=i+1;j<n;j++)
{
if(min>a[j])
{
min=a[j];
loc=j;
}
}
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
cout<<"Sorted list = ";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
getch();
}
OUTPUT-
Q. Write a class Play in C++ with the following specifications
Private members
1) Play code integer
2) Play Title 25 character
3) Duration float
4) Noofscenes integer
Public member function of class Play
1) A constructor function to initialise Duration as 45 and Noofscenes
as 5.
2) Moreinfo() function to assign the values of Duration and
Noofscenes with the help of corresponding values passed as
parameters to this function.
3) Newplay() function to accept values for Playcode and Playtitle.
4) Shoplay() function to display all data members on the screen.

Ans-

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

class Play
{
int playcode;
char playtitle[25];
float duration;
int noofscenes;
public:
Play()
{
duration=45.0;
noofscenes=5;
}
void newplay()
{
cout<<"Enter playcode = ";
cin>>playcode;
cout<<"Enter playtitle = ";
cin>>playtitle;
}
void Moreinfo(float d,int n)
{
duration=d;
noofscenes=n;
}
void Showplay()
{
cout<<"\nPlaycode = "<<playcode;
cout<<"\nPlaytitle = "<<playtitle;
cout<<"\nDuration = "<<duration;
cout<<"\nNo. of scenes = "<<noofscenes;
}
};

void main()
{
clrscr();
Play p1;
p1.newplay();
p1.Moreinfo(5,15);
p1.Showplay();
getch();
}

OUTPUT-

You might also like