C++ Programming
Slip15
Q.1) Write a C++ program to create a class which contains two data members. Write member
functions to accept display and swap two entered numbers using call by reference.
[Marks 15]
Solution
#include<iostream.h>
#include<conio.h>
class change
{
private:
int a,b;
public:
void accept(int x,int y);
void swap(int *a,int *b);
void display(int a,int b);
};
void change:: accept(int x,int y)
{
a=x;
b=y;
}
void change:: swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void change:: display(int a,int b)
{
cout<<"\nAfter swap:"<<a<<"\t"<<b;
}
void main()
{
change s1;
int x,y;
clrscr();
cout<<"Enter the value of x & y:";
cin>>x>>y;
s1.accept(x,y);
cout<<"\nBefore swap:"<<x<<"\t"<<y;
s1.swap(&x,&y);
s1.display(x,y);
getch();
}
Output
Enter the value of x & y:
10
20
Before swap:10 20
After swap:20 10
Q.2) Create a base class Student(Roll_No, Name, Class) which derives two classes
Internal_Marks (IntM1, IntM2, IntM3, IntM4, IntM5) and External_Marks(ExtM1
ExtM2, ExtM3, ExtM4, ExtM5 ). Class Result(T1, T2, T3, T4, T5) inherits both
Internal_Marks and External_Marks classes. (Use Virtual Base Class)
Write a C++ menu driven program to perform the following functions:
i. To Accept and display student details
ii. Calculate Subject wise total marks obtained. Check whether student has
passed in Internal and External Exam of each subject. Also check whether
he has passed in respective subject or not and display result accordingly.
[Marks 25]
Solution
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h>
class student
{
protected :
int roll_no;
char name[20];
char clas[10];
public:
void acceptS()
{
cout<<"\n\t Enter roll no :";
cin>>roll_no;
cout<<"\n\t Enter Name of Student :";
cin>>name;
cout<<"\n\t Enter Class : ";
cin>>clas;
}
void displayS()
{
cout<<"\t Roll No: "<<roll_no<<endl;
cout<<"\t Name: "<<name<<endl;
cout<<"\t class: "<<clas<<endl;
}
};
class internal_marks : virtual public student
{
protected :
int intM1,intM2,intM3, intM4, intM5;
public :
void acceptIM()
{
cout<<"\n\t Enter Internal marks of student"<<endl;
cout<<"\t Marks1 : ";
cin>>intM1;
cout<<"\t Marks2 : ";
cin>>intM2;
cout<<"\t Marks3 : ";
cin>>intM3;
cout<<"\t Marks4 : ";
cin>>intM4;
cout<<"\t Marks5 : ";
cin>>intM5;
}
void displayIM()
{
cout<<"\t Internal Marks Of Student : "<<endl;
cout<<"\t Marks1 : "<<intM1;
if(intM1<8)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
cout<<"\t Marks2 : "<<intM2;
if(intM2<8)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
cout<<"\t Marks3 : "<<intM3;
if(intM3<8)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
cout<<"\t Marks4 : "<<intM4;
if(intM4<8)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
cout<<"\t Marks5 : "<<intM5;
if(intM5<8)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
}
};
class external_marks : virtual public student
{
protected :
int extM1, extM2, extM3, extM4, extM5;
public :
void acceptEM()
{
cout<<"\t Enter External marks of student"<<endl;
cout<<"\t Marks1 : ";
cin>>extM1;
cout<<"\t Marks2 : ";
cin>>extM2;
cout<<"\t Marks3 : ";
cin>>extM3;
cout<<"\t Marks4 : ";
cin>>extM4;
cout<<"\t Marks5 : ";
cin>>extM5;
}
void displayEM()
{
cout<<"\t External Marks Of Student : "<<endl;
cout<<"\t Marks1 : "<<extM1;
if(extM1<32)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
cout<<"\t Marks2 : "<<extM2;
if(extM2<32)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
cout<<"\t Marks3 : "<<extM3;
if(extM3<32)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
cout<<"\t Marks4 : "<<extM4;
if(extM4<32)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
cout<<"\t Marks5 : "<<extM5;
if(extM5<32)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
}
};
class result : public internal_marks, public external_marks
{
private:
int T1,T2,T3,T4,T5;
public: void clacT()
{ cout<<"\t Total Marks : "<<endl;
T1=intM1+extM1;
cout<<"\t Total 1 : "<<T1;
if(T1<40 || intM1<8 || extM1<32)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
T2=intM2+extM2;
cout<<"\t Total 2 : "<<T2;
if(T2<40 || intM2<8 || extM2<32)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
T3=intM3+extM3;
cout<<"\t Total 3 : "<<T3;
if(T3<40 || intM3<8 || extM3<32)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
T4=intM4+extM4;
cout<<"\t Total 4 : "<<T4;
if(T4<40 || intM4<8 || extM4<32)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
T5=intM5+extM5;
cout<<"\t Total 5 : "<<T5;
if(T5<40 || intM5<8 || extM5<32)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
}
};
int main()
{ clrscr();
result r;
int c;
do
{
cout<<"\n\t 1. To Accept and Display student details";
cout<<"\n\t 2. Calculate subject wise total marks";
cout<<"\n\t 3. Exit";
cout<<"\n\t Enter your choice : ";
cin>>c;
switch(c)
{
case 1: r.acceptS();
cout<<"\t--------------------------------------"<<endl;
r.acceptIM();
cout<<"\t--------------------------------------";
r.acceptEM();
cout<<"\t--------------------------------------";
break;
case 2: r.displayS();
cout<<"\t--------------------------------------"<<endl;
r.displayIM();
cout<<"\t--------------------------------------"<<endl;
r.displayEM();
cout<<"\t--------------------------------------"<<endl;
r.clacT();
cout<<"\t--------------------------------------"<<endl;
break;
}
}while(c!=3);
getch();
return 0;
}
Output
1. To Accept and Display student details
2. Calculate subject wise total marks
3. Exit
Entre your choice:=
Enter Roll No:1
Enter Name of Student :Akshay
Enter Class : 12
--------------------------------------
Enter Internal marks of student
Marks1 : 18
Marks2 : 17
Marks3 : 16
Marks4 : 15
Marks5 : 19
--------------------------------------
Enter External marks of student
Marks1 : 75
Marks2 : 74
Marks3 : 73
Marks4 : 72
Marks5 : 76
--------------------------------------
Total Marks :
Total 1 : 93 Pass
Total 2 : 91 Pass
Total 3 : 89 Pass
Total 4 : 87 Pass
Total 5 : 95 Pass
--------------------------------------
1. To Accept and Display student details
2. Calculate subject wise total marks
3. Exit
Enter your choice : 3
Q.1) Write a C++ program to create a class which contains two data members. Write member
functions to accept display and swap two entered numbers using call by reference.
[Marks 15]
Solution
#include<iostream.h>
#include<conio.h>
class change
{
private:
int a,b;
public:
void accept(int x,int y);
void swap(int *a,int *b);
void display(int a,int b);
};
void change:: accept(int x,int y)
{
a=x;
b=y;
}
void change:: swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void change:: display(int a,int b)
{
cout<<"\nAfter swap:"<<a<<"\t"<<b;
}
void main()
{
change s1;
int x,y;
clrscr();
cout<<"Enter the value of x & y:";
cin>>x>>y;
s1.accept(x,y);
cout<<"\nBefore swap:"<<x<<"\t"<<y;
s1.swap(&x,&y);
s1.display(x,y);
getch();
}
Output
Enter the value of x & y:
10
20
Before swap:10 20
After swap:20 10
Q.2) Create a base class Student(Roll_No, Name, Class) which derives two classes
Internal_Marks (IntM1, IntM2, IntM3, IntM4, IntM5) and External_Marks(ExtM1
ExtM2, ExtM3, ExtM4, ExtM5 ). Class Result(T1, T2, T3, T4, T5) inherits both
Internal_Marks and External_Marks classes. (Use Virtual Base Class)
Write a C++ menu driven program to perform the following functions:
i. To Accept and display student details
ii. Calculate Subject wise total marks obtained. Check whether student has
passed in Internal and External Exam of each subject. Also check whether
he has passed in respective subject or not and display result accordingly.
[Marks 25]
Solution
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h>
class student
{
protected :
int roll_no;
char name[20];
char clas[10];
public:
void acceptS()
{
cout<<"\n\t Enter roll no :";
cin>>roll_no;
cout<<"\n\t Enter Name of Student :";
cin>>name;
cout<<"\n\t Enter Class : ";
cin>>clas;
}
void displayS()
{
cout<<"\t Roll No: "<<roll_no<<endl;
cout<<"\t Name: "<<name<<endl;
cout<<"\t class: "<<clas<<endl;
}
};
class internal_marks : virtual public student
{
protected :
int intM1,intM2,intM3, intM4, intM5;
public :
void acceptIM()
{
cout<<"\n\t Enter Internal marks of student"<<endl;
cout<<"\t Marks1 : ";
cin>>intM1;
cout<<"\t Marks2 : ";
cin>>intM2;
cout<<"\t Marks3 : ";
cin>>intM3;
cout<<"\t Marks4 : ";
cin>>intM4;
cout<<"\t Marks5 : ";
cin>>intM5;
}
void displayIM()
{
cout<<"\t Internal Marks Of Student : "<<endl;
cout<<"\t Marks1 : "<<intM1;
if(intM1<8)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
cout<<"\t Marks2 : "<<intM2;
if(intM2<8)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
cout<<"\t Marks3 : "<<intM3;
if(intM3<8)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
cout<<"\t Marks4 : "<<intM4;
if(intM4<8)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
cout<<"\t Marks5 : "<<intM5;
if(intM5<8)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
}
};
class external_marks : virtual public student
{
protected :
int extM1, extM2, extM3, extM4, extM5;
public :
void acceptEM()
{
cout<<"\t Enter External marks of student"<<endl;
cout<<"\t Marks1 : ";
cin>>extM1;
cout<<"\t Marks2 : ";
cin>>extM2;
cout<<"\t Marks3 : ";
cin>>extM3;
cout<<"\t Marks4 : ";
cin>>extM4;
cout<<"\t Marks5 : ";
cin>>extM5;
}
void displayEM()
{
cout<<"\t External Marks Of Student : "<<endl;
cout<<"\t Marks1 : "<<extM1;
if(extM1<32)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
cout<<"\t Marks2 : "<<extM2;
if(extM2<32)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
cout<<"\t Marks3 : "<<extM3;
if(extM3<32)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
cout<<"\t Marks4 : "<<extM4;
if(extM4<32)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
cout<<"\t Marks5 : "<<extM5;
if(extM5<32)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
}
};
class result : public internal_marks, public external_marks
{
private:
int T1,T2,T3,T4,T5;
public: void clacT()
{ cout<<"\t Total Marks : "<<endl;
T1=intM1+extM1;
cout<<"\t Total 1 : "<<T1;
if(T1<40 || intM1<8 || extM1<32)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
T2=intM2+extM2;
cout<<"\t Total 2 : "<<T2;
if(T2<40 || intM2<8 || extM2<32)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
T3=intM3+extM3;
cout<<"\t Total 3 : "<<T3;
if(T3<40 || intM3<8 || extM3<32)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
T4=intM4+extM4;
cout<<"\t Total 4 : "<<T4;
if(T4<40 || intM4<8 || extM4<32)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
T5=intM5+extM5;
cout<<"\t Total 5 : "<<T5;
if(T5<40 || intM5<8 || extM5<32)
cout<<"\tFail *"<<endl;
else
cout<<"\tPass"<<endl;
}
};
int main()
{ clrscr();
result r;
int c;
do
{
cout<<"\n\t 1. To Accept and Display student details";
cout<<"\n\t 2. Calculate subject wise total marks";
cout<<"\n\t 3. Exit";
cout<<"\n\t Enter your choice : ";
cin>>c;
switch(c)
{
case 1: r.acceptS();
cout<<"\t--------------------------------------"<<endl;
r.acceptIM();
cout<<"\t--------------------------------------";
r.acceptEM();
cout<<"\t--------------------------------------";
break;
case 2: r.displayS();
cout<<"\t--------------------------------------"<<endl;
r.displayIM();
cout<<"\t--------------------------------------"<<endl;
r.displayEM();
cout<<"\t--------------------------------------"<<endl;
r.clacT();
cout<<"\t--------------------------------------"<<endl;
break;
}
}while(c!=3);
getch();
return 0;
}
Output
1. To Accept and Display student details
2. Calculate subject wise total marks
3. Exit
Entre your choice:=
Enter Roll No:1
Enter Name of Student :Akshay
Enter Class : 12
--------------------------------------
Enter Internal marks of student
Marks1 : 18
Marks2 : 17
Marks3 : 16
Marks4 : 15
Marks5 : 19
--------------------------------------
Enter External marks of student
Marks1 : 75
Marks2 : 74
Marks3 : 73
Marks4 : 72
Marks5 : 76
--------------------------------------
Total Marks :
Total 1 : 93 Pass
Total 2 : 91 Pass
Total 3 : 89 Pass
Total 4 : 87 Pass
Total 5 : 95 Pass
--------------------------------------
1. To Accept and Display student details
2. Calculate subject wise total marks
3. Exit
Enter your choice : 3
Tags:
c++