Codetantra
Codetantra
Aim:
Page No: 1
Write a C++ program to find the sum of individual digits of a positive integer.
Source Code:
ID: 22SCSE1012705
sum.cpp
#include <iostream>
using namespace std;
int main(){
int a,sum=0;
cout<< "Enter a number: ";
cin>> a;
while (a!=0){
sum +=a%10;
a=a/10;
}
E2UC201C-2022-Section-43
cout<< "Sum is= "<<sum<<endl;
}
User Output
Aim:
Page No: 2
Write a C++ program to read an array of integers (with max size 10) and print the largest and the
smallest of the given numbers.
Print the output as shown in the test cases. Note: Do use the printf() function with a newline character ( \n ) at
ID: 22SCSE1012705
the end.
Source Code:
ArraysDemo5.cpp
#include <iostream>
using namespace std;
int main(){
int n,i,min,max,arr[10];
cout<< "Enter n : ";
cin>> n;
cout<< "Enter "<<n<<" Values : ";
for(i=0;i<n;i++){
E2UC201C-2022-Section-43
cin>>arr[i];
}
max= min =arr[0] ;
for(i=0;i<n;i++){
if(max<arr[i]) max = arr[i];
if(min>arr[i]) min = arr[i];
}
cout<<"Largest element = "<<max<<endl<<"Smallest element = "<<min<<endl;
}
User Output
Enter n :
4
Enter 4 Values :
24 38 15 13
Largest element = 38
Smallest element = 13
Test Case - 2
User Output
Enter n :
5
Enter 5 Values :
-1 -2 -3 -44 -33
Largest element = -1
Smallest element = -44
Aim:
Page No: 4
Write a C++ program to overload new and delete operators as member functions to allocate memory to
the class and destroy it.
Note: Write a class Student which contains two members name and id , a constructor and overloaded
ID: 22SCSE1012705
operator functions new and delete in the below code.
Note: Driver code is given in OverloadNewDelete2.cpp tab and you have to complete the code in
OverloadNewDelete2a.cpp tab.
Source Code:
E2UC201C-2022-Section-43
OverloadNewDelete2.cpp
#include <iostream>
using namespace std;
#include "OverloadNewDelete2a.cpp"
int main() {
Student *s;
s = new Student("Saraswathi", 555);
s -> display();
delete s;
return 0;
OverloadNewDelete2a.cpp
User Output
Page No: 5
The student name : Saraswathi
The student id : 555
ID: 22SCSE1012705
E2UC201C-2022-Section-43
Galgotias University Greater Noida
Exp. Name: C++ program that implement bubble sort, to
S.No: 4 Date: 2023-02-26
sort a given list of integer in ascending order
Aim:
Page No: 6
Write a C++ program that implement bubble sort, to sort a given list of integer in ascending order.
Source Code:
ID: 22SCSE1012705
BubbleSort.cpp
#include <iostream>
using namespace std;
int main(){
int i,j,n,arr[10],temp;
cout<<"Enter the number of data element to be sorted : ";
cin>> n;
for(i=0;i<n;i++){
cout<< "Enter element "<<i+1<<": ";
cin>> arr[i];
}
E2UC201C-2022-Section-43
for(i=0;i<n;i++){
for(j=0;j<n-i-1;j++){
if(arr[j]>arr[j+1]){
temp = arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<< "Sorted Data";
User Output
Enter the number of data element to be sorted :
5
Enter element 1:
45
Enter element 2:
12
Enter element 3:
89
Enter element 4:
0
Enter element 5:
1
Page No: 7
Sorted Data 0 1 12 45 89
Test Case - 2
ID: 22SCSE1012705
User Output
Enter the number of data element to be sorted :
8
Enter element 1:
100
Enter element 2:
60
Enter element 3:
0
Enter element 4:
E2UC201C-2022-Section-43
23
Enter element 5:
2
Enter element 6:
1
Enter element 7:
78
Enter element 8:
999
Test Case - 3
User Output
Enter the number of data element to be sorted :
3
Enter element 1:
10001
Enter element 2:
999
Enter element 3:
200
Sorted Data 200 999 10001
S.No: 5 Exp. Name: Write the code to concatenate two strings Date: 2023-02-26
Aim:
Page No: 8
Write a C++ program illustrating user-defined string processing functions using pointers to concatenate two
strings.
Source Code:
ID: 22SCSE1012705
concatenation.cpp
#include <iostream>
using namespace std;
string concat(string* temp1,string* temp2){
*temp1 = *temp1+ *temp2;
return *temp1;
}
int main(){
string temp1,temp2;
cout<<"enter first string: ";
cin>>temp1;
E2UC201C-2022-Section-43
cout<<"enter second string: ";
cin>>temp2;
cout<<"The concatenated string is "<<concat(&temp1,&temp2)<<endl;
}
Test Case - 2
User Output
enter first string:
cpp
enter second string:
programming
The concatenated string is cppprogramming
S.No: 6 Exp. Name: Write the code to copy a string Date: 2023-02-26
Aim:
Page No: 9
Write a C++ program illustrating user-defined string processing functions using pointers to copy a string.
Source Code:
ID: 22SCSE1012705
copyString.cpp
#include <iostream>
using namespace std;
int main(){
char temp1[50];
cout<<"enter a string: ";
gets(temp1);
cout<<"target string: "<< temp1<<endl;
}
E2UC201C-2022-Section-43
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
enter a string:
c++ programming
target string: c++ programming
User Output
enter a string:
CodeTantra
target string: CodeTantra
Exp. Name: Write the code to find string length using
S.No: 7 Date: 2023-02-26
pointers
Aim:
Page No: 10
Write a C++ program illustrating user-defined string processing functions using pointers to find string length.
Source Code:
ID: 22SCSE1012705
stringLength.cpp
#include <iostream>
using namespace std;
int length(string temp){
int i = 0,count = 0;
while (temp[i]!= NULL){
count ++;
i++;
}
return count;
}
E2UC201C-2022-Section-43
int main(){
string temp;int result;
cout<<"enter the string: ";
cin>> temp;
result = length(temp);
cout<<result<<endl;
}
User Output
enter the string:
stringlength
12
Test Case - 2
User Output
enter the string:
Cppprogramming
14
Exp. Name: Write a C++ program to make a simple
S.No: 8 Calculator to Add, Subtract, Multiply or Divide using Date: 2023-03-05
switch-case
Page No: 11
Aim:
Write a program to read two integer values and an arithmetic operator, depending on the operator perform
different arithmetic operations.
ID: 22SCSE1012705
If integer values 2 and 3 are given with operator +, then the output should be 2 + 3 = 5.
If integer values 6 and 3 are given with operator /, then the output should be 6 / 3 = 2.
If other than arithmetic operator is given, then display "Error! Operator is not correct".
At the time of execution, the program should print the message on the console as:
Enter two integer values :
For example, if the user gives the input as:
Enter two integer values : 12 10
Next, the program should print the message on the console as:
Enter an arithmetic operator :
E2UC201C-2022-Section-43
For example, if the user gives the input as:
Enter an arithmetic operator : +
then the program should print the result as:
12 + 10 = 22
Note: Do use newline character (\n) at the end.
Source Code:
SwitchCaseDemo3.cpp
Page No: 12
cin>> num1>>num2;
cout<< "Enter an arithmetic operator : ";
cin>> oper;
switch(oper){
case '+':
ID: 22SCSE1012705
result = num1 + num2;
break;
case '-':
result = num1- num2;
break;
case '*':
result = num1*num2;break;
case '/':
result = num1/num2;break;
case '%':
result = num1 % num2;break;
default:
E2UC201C-2022-Section-43
cout<< "Error! Operator is not correct"<<endl;return 0;
}
cout<< num1<<" "<<oper<<" "<< num2<<" = "<<result<<endl;
}
Test Case - 2
User Output
Enter two integer values :
58
Enter an arithmetic operator :
*
5 * 8 = 40
Test Case - 3
User Output
Enter two integer values :
123 12
Enter an arithmetic operator :
%
123 % 12 = 3
Page No: 13
Test Case - 4
User Output
ID: 22SCSE1012705
Enter two integer values :
67 89
Enter an arithmetic operator :
#
Error! Operator is not correct
E2UC201C-2022-Section-43
Galgotias University Greater Noida
S.No: 9 Exp. Name: Program to illustrate default constructor Date: 2023-03-05
Aim:
Page No: 14
Fill the below missing c++ program to calculate the area of the rectangle wall using the default constructor.
Source Code:
default.cpp
ID: 22SCSE1012705
#include <iostream>
using namespace std;
// declare a class
class Wall {
private:
//declare the variables
float len;
float breadth;
public:
E2UC201C-2022-Section-43
// default constructor to initialize variable
Wall() {
cout<<"Enter the length: ";
cin>> len;
cout<<"Enter the breadth: ";
cin>> breadth;
cout<<"Area = "<< len*breadth<<endl;
}
};
int main() {
User Output
Enter the length:
10
Enter the breadth:
8
Area = 80
Test Case - 2
User Output
Enter the length:
69.25
26.89
Area = 1862.13
Enter the breadth:
Aim:
Page No: 16
Fill in the below missing C++ program to demonstrate the student details using copy constructor
Source Code:
copy.cpp
ID: 22SCSE1012705
#include <iostream>
#include <string.h>
using namespace std;
class student {
//declare variables...
int roll;
string name;
public:
student(int Roll, string Name);
student(student &obj) // copy constructor
E2UC201C-2022-Section-43
{
roll = obj.roll;
name = obj.name;
}
void display();
};
return 0;
}
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Page No: 17
Enter roll number:
25
Enter student name:
Karna
ID: 22SCSE1012705
25 Karna
25 Karna
E2UC201C-2022-Section-43
Galgotias University Greater Noida
Exp. Name: Program to illustrate parameterized
S.No: 11 Date: 2023-03-05
constructor
Aim:
Page No: 18
Fill in the below missing c++ program to calculate the area of the rectangle wall using the parameterized
constructor.
Source Code:
ID: 22SCSE1012705
parameterized.cpp
#include<iostream>
using namespace std;
class wall
{
private:
//declare variables...
int length;
int breadth;
E2UC201C-2022-Section-43
public:
wall (int len, int brth) //parameterized constructor to initialize l and b
{
length = len;
breadth = brth;
}
int area( ) //function to find area
{
return length*breadth;
}
void display() //function to display the area
int main()
{
int l,b;
cout<<"Enter length: ";
cin>>l;
cout<<"Enter breadth: ";
cin>>b;
wall c(l,b); //initializing the data members of object 'c' implicitly
//call area function
c.area();
//call display function
c.display();
return 0;
} //end of program
User Output
Enter length:
25
Page No: 19
Enter breadth:
40
Area = 1000
ID: 22SCSE1012705
Test Case - 2
User Output
Enter length:
36
Enter breadth:
52
Area = 1872
E2UC201C-2022-Section-43
Galgotias University Greater Noida
Exp. Name: Write a Program to Implement a Class
S.No: 12 Date: 2023-03-05
STUDENT having the Following Members:
Aim:
Page No: 20
Write a Program to Implement a Class STUDENT having the Following Members:
MEMBER DESCRIPTION
Data Members
ID: 22SCSE1012705
Sname Name of the student
Marks array Marks of the students
Total Total marks obtained
TMax Total Maximum marks
Member Functions
Assign() Assign initial Values
Compute() To compute total and average
Display() To display the data
Source Code:
E2UC201C-2022-Section-43
student.cpp
Page No: 21
string Sname;
int Marks[subs],TMax,i;
float Total=0;
public:
void Assign(){
ID: 22SCSE1012705
cout<< "Enter Student Name: ";
cin>> Sname;
for(i=0; i< subs; i++){
cout<<"Enter marks of subject "<<i+1<<": ";
cin>>Marks[i];
}
cout<< "Enter maximum total marks: ";
cin>>TMax;
}
float Compute(){
for(i=0;i<subs;i++){
Total += Marks[i];
E2UC201C-2022-Section-43
}
}
void Display(){
cout<<"Student Name: "<<Sname<<endl<<"Marks are"<<endl;
for(i=0;i<subs;i++){
cout<<"Subject "<<i+1<<" : "<<Marks[i]<<endl;
}
cout<<"Total: "<<Total<<endl;
cout<<"Percentage: "<<Total/subs<<endl;
}
};
User Output
Enter Student Name:
Arjun
Enter marks of subject 1:
95
Enter marks of subject 2:
26
Enter marks of subject 3:
53
Enter marks of subject 4:
84
Enter marks of subject 5:
95
Page No: 22
Enter marks of subject 6:
74
Enter maximum total marks:
600
ID: 22SCSE1012705
Student Name: Arjun
Marks are
Subject 1 : 95
Subject 2 : 26
Subject 3 : 53
Subject 4 : 84
Subject 5 : 95
Subject 6 : 74
Total: 427
Percentage: 71.1667
E2UC201C-2022-Section-43
Galgotias University Greater Noida
Exp. Name: Write a C++ program to Subtract two
S.No: 13 Date: 2023-03-05
Complex numbers by Overloading - operator
Aim:
Page No: 23
Write a C++ program to overload the binary - operator as a member function to subtract two complex numbers.
Source Code:
ID: 22SCSE1012705
binaryMinus.cpp
#include <iostream>
using namespace std;
class complex {
private:
int real,imag;
public:
complex(){
real = 0;
imag = 0;
E2UC201C-2022-Section-43
}
complex(int Real, int Imag){
real = Real;
imag = Imag;
}
complex operator -(complex c){
complex temp;
temp.real = real - c.real;
temp.imag = imag - c.imag;
return temp;
}
User Output
Enter real and imaginary parts :
85
Enter real and imaginary parts :
36
Subtraction of two complex numbers : 5 + i-1
Page No: 24
Test Case - 2
User Output
ID: 22SCSE1012705
Enter real and imaginary parts :
8 -9
Enter real and imaginary parts :
5 -2
Subtraction of two complex numbers : 3 + i-7
E2UC201C-2022-Section-43
Galgotias University Greater Noida
Exp. Name: Write a C++ program to find Addition of
S.No: 14 Date: 2023-03-05
different Data type values using Function Overloading
Aim:
Page No: 25
Write a C++ program to find the addition of two integer values, two float values and two character values
using function overloading.
At the time of execution, the program should print the following messages one by one on the console as:
ID: 22SCSE1012705
Enter two integer values :
Enter two float values :
Enter two char values :
E2UC201C-2022-Section-43
Sum of two integers : 57
Sum of two floats : 91.346
Sum of two characters : z
Note: Do use the cout with a newline character to display the output.
Source Code:
FunctionOverloading2.cpp
#include <iostream>
using namespace std;
int main() {
int a, b;
float c, d;
char p, q;
cout << "Enter two integer values : ";
cin >> a >> b;
cout << "Enter two float values : ";
cin >> c >> d;
cout << "Enter two char values : ";
cin >> p >> q;
cout << "Sum of two integers : " << add(a, b) << endl;
cout << "Sum of two floats : " << add(c, d) << endl;
cout << "Sum of two characters : " << add(p, q) << endl;
}
Add.h
// Write function definitions for add()
int add(int a, int b){
return a+b;
}
float add(float a, float b){
Page No: 26
return a+b;
}
char add(char a, char b){
return a+b;
}
ID: 22SCSE1012705
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Enter two integer values :
23 56
Enter two float values :
E2UC201C-2022-Section-43
3.45 7.18
Enter two char values :
A4
Sum of two integers : 79
Sum of two floats : 10.63
Sum of two characters : u
Aim:
Page No: 27
Write a program illustrate function overloading .
Write two overloading functions for power(number, pwr) where number is of int argument or double
argument and pwr is an int argument .
ID: 22SCSE1012705
Let us consider the default argument value for pwr is 2.
At the time of execution, the program should print the following messages one by one on the console as:
E2UC201C-2022-Section-43
Enter any double value : 2.5
Enter the power value : 5
The square of 3 : 9
The cube of 3 : 27
The 3 to the power of 5 : 243
The square of 2.5 : 6.25
The cube of 2.5 : 15.625
The 2.5 to the power of 5 : 97.6562
FunOverloading.cpp
#include <iostream>
#include <cmath>
using namespace std;
int power(int number , int pwr){
return pow(number, pwr);
Page No: 28
}
double power(double number, int pwr){
return pow(number, pwr);
}
int main(){
ID: 22SCSE1012705
int num1,pwr;
double num2;
cout<< "Enter any integer value : ";
cin>> num1;
cout<< "Enter any double value : ";
cin>> num2;
cout<< "Enter the power value : ";
cin>> pwr;
cout<< "The square of "<< num1 <<" : "<< power(num1,2)<<endl;
cout<< "The cube of "<< num1 << " : "<< power(num1,3)<<endl;
cout<< "The "<<num1<<" to the power of "<< pwr<<" : "<<power(num1,pwr)<<endl;
cout<< "The square of "<< num2 << " : "<< power(num2,2)<<endl;
E2UC201C-2022-Section-43
cout<< "The cube of "<< num2<< " : "<< power(num2,3)<<endl;
cout<< "The "<<num2<<" to the power of "<<pwr<<" : "<<power(num2,pwr)<<endl;
}
User Output
Aim:
Page No: 29
Write a C++ program to exchange two private data members of different classes.
At the time of execution, the program should print the message on the console as:
ID: 22SCSE1012705
For example, if the user gives the input as:
Next, the program should print the message on the console as:
E2UC201C-2022-Section-43
then the program should print the result as:
FriendFunctions3.cpp
exchange.h
// remember the main use here is of &.
// friend type funcName(class& classInstance);
class Sample{
private:
int data;
Page No: 30
public:
void getData(){
cout<< "Enter first value : ";
cin>> data;
}
ID: 22SCSE1012705
void setData(int Data){
data = Data;
}
friend int sampledemo(Sample& s);
};
class Test {
private:
int data;
public:
void getData(){
cout<<"Enter second value : ";
cin>> data;
E2UC201C-2022-Section-43
}
void setData(int Data){
data = Data;
}
friend int testdemo(Test& t);
};
int sampledemo(Sample& s){
return s.data;
}
int testdemo(Test& t){
return t.data;
User Output
Enter first value :
99
Enter second value :
98
Aim:
Page No: 32
Your task is to Create:
• The class "ONE" contains two member variables: "private_variable" and "protected_variable".
• The class "TWO" is declared as a friend class of "ONE". This allows the members of class "TWO" to access
the private and protected members of class "ONE".
ID: 22SCSE1012705
• In the main function, objects of classes "ONE" and "TWO" are created and the "display" function of class
"TWO" is called and passed an object of class "ONE".
• The "display" function then displays the values of the private and protected member variables of the object
of class "ONE".
Source Code:
friendclass.cpp
#include <iostream>
using namespace std;
E2UC201C-2022-Section-43
class ONE {
private:
int a;
protected:
int b;
public:
ONE(int x,int y){
a=x;
b=y;
}
class TWO {
public:
void display(ONE t){
cout<<"The value of Private Variable = "<< t.a<<endl;
cout<<"The value of Protected Variable = "<< t.b<<endl;
}
};
// Driver code
int main()
{
int a,b;
cin>>a;
cin>>b;
ONE t1(a,b);
TWO t2;
t2.display(t1);
}
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Page No: 33
10
20
The value of Private Variable = 10
The value of Protected Variable = 20
ID: 22SCSE1012705
Test Case - 2
User Output
50
60
The value of Private Variable = 50
The value of Protected Variable = 60
E2UC201C-2022-Section-43
Galgotias University Greater Noida
Exp. Name: Write a Program to Access Members of a
S.No: 18 Date: 2023-03-06
STUDENT Class Using Pointer to Object Members.
Aim:
Page No: 34
Write a Program to Access Members of a STUDENT Class Using Pointer to Object Members.
Source Code:
ID: 22SCSE1012705
student_class.cpp
#include <iostream>
using namespace std;
class STUDENT{
private:
string Name;
int Age;
double Grade;
public:
STUDENT(string name, int age, double grade){
Name = name;
E2UC201C-2022-Section-43
Age = age;
Grade = grade;
}
string getname(){
return Name;
}
int getage(){
return Age;
}
double getgrade(){
return Grade;
Page No: 35
Name: JohnDoe
Age: 22
Grade: 85.4
ID: 22SCSE1012705
Test Case - 2
User Output
george
22
68.9
Name: george
Age: 22
Grade: 68.9
E2UC201C-2022-Section-43
Galgotias University Greater Noida
Exp. Name: Write a C++ program to find Subject Totals
S.No: 22 and Average Marks of a Student using Multiple Date: 2023-03-06
Inheritance
Page No: 36
Aim:
Write a C++ program to
• define a base class Internals contains internal marks of 3 subjects
• define another base class Externals contains external marks of 3 subjects
• define a class Result derived from Internals and Externals , which finds 3 subject totals, total
ID: 22SCSE1012705
marks and average marks.
At the time of execution, the program should print the message on the console as:
Next, the program should print the message on the console as:
E2UC201C-2022-Section-43
If the user gives the input as:
MultipleInheritance2.cpp
#include <iostream>
using namespace std;
#include "MultipleInheritance2a.cpp"
class Result : public Internals, public Externals {
private:
Page No: 37
float s1, s2, s3, tot, avg;
public:
void displayTotAvg();
};
#include "MultipleInheritance2b.cpp"
ID: 22SCSE1012705
int main() {
Result r;
r.readInternals();
r.readExternals();
r.displayTotAvg();
return 0;
}
MultipleInheritance2a.cpp
// Implement class Internals and externals as required
E2UC201C-2022-Section-43
class Internals {
protected:
float i1, i2, i3;
public:
void readInternals(){
cout<<"Enter internal marks of 3 subjects : ";
cin>> i1>>i2>>i3;
}
};
class Externals {
protected:
MultipleInheritance2b.cpp
// Implement displayToAvg function of Result class here
void Result :: displayTotAvg(){
s1 = i1+ e1;
s2 = i2+ e2;
s3 = i3+ e3;
tot = s1+s2+s3;
avg = tot /3;
cout<< "Three subject totals : "<<s1<<" "<<s2<<" "<<s3<<endl;
cout<< "Total marks : "<<tot<<endl;
cout<< "Average marks : "<<avg<<endl;
}
Execution Results - All test cases have succeeded!
Test Case - 1
User Output
Page No: 38
Enter internal marks of 3 subjects :
25.75 24.5 29.5
Enter external marks of 3 subjects :
67 61 54.75
ID: 22SCSE1012705
Three subject totals : 92.75 85.5 84.25
Total marks : 262.5
Average marks : 87.5
E2UC201C-2022-Section-43
Galgotias University Greater Noida
Exp. Name: Write a C++ program on find Square and
S.No: 23 Date: 2023-03-06
Cube of a given number using Hierarchical Inheritance
Aim:
Page No: 39
Write a C++ program to
• define a base class Number contains an integer number
• define a derived class Square derived from Number to find square of a given number
• define another derived class Cube derived from Number to find cube of a given number
At the time of execution, the program should print the message on the console as:
ID: 22SCSE1012705
Enter an integer number :
The square of 5 is : 25
Next, the program should print the message on the console as:
E2UC201C-2022-Section-43
Enter an integer number :
HierarchicalInheritance2.cpp
// Uneditable driver code
#include <iostream>
using namespace std;
#include "HierarchicalInheritance2a.cpp"
class Square : public Number {
Page No: 40
public:
int getSquare();
};
class Cube : public Number {
public:
ID: 22SCSE1012705
int getCube();
};
#include "HierarchicalInheritance2b.cpp"
int main() {
Square s;
s.readNumber();
cout << "The square of " << s.getNumber() << " is : " << s.getSquare() << endl;
Cube c;
c.readNumber();
cout << "The cube of " << c.getNumber() << " is : " << c.getCube() << endl;
return 0;
}
E2UC201C-2022-Section-43
HierarchicalInheritance2a.cpp
// Write your code here for Number class
class Number {
private:
int num;
public:
void readNumber(){
cout<<"Enter an integer number : ";
HierarchicalInheritance2b.cpp
// Implement members functionality of Square and Cube class
int Square :: getSquare(){
int num = getNumber();
return num*num;
}
int Cube :: getCube(){
int num = getNumber();
return num*num*num;
}
User Output
Enter an integer number :
5
Page No: 41
The square of 5 is : 25
Enter an integer number :
6
The cube of 6 is : 216
ID: 22SCSE1012705
Test Case - 2
User Output
Enter an integer number :
11
The square of 11 is : 121
Enter an integer number :
11
The cube of 11 is : 1331
E2UC201C-2022-Section-43
Galgotias University Greater Noida
Exp. Name: Write a C++ program to find Total, Average
S.No: 24 Date: 2023-03-06
and Grade of a Student using Multilevel Inheritance
Aim:
Page No: 42
Write a C++ program to
• define a base class Student contains student id and name
• define a class Test derived from Student , contains marks of 3 subjects
• define a class Result derived from Test , which finds total, average and grade of a Test.
The grades of the Test are:
ID: 22SCSE1012705
• >= 75% - Distinction
• >= 60% and < 75% - First class
• >= 50% and < 60% - Second class
• >= 35% and < 50% - Third class
• < 35% - Very poor in studies
At the time of execution, the program should print the message on the console as:
E2UC201C-2022-Section-43
Next, the program should print the message on the console as:
MultilevelInheritance2.cpp
// Uneditable driver code
#include <iostream>
using namespace std;
class Student {
private:
Page No: 43
int id;
char name[30];
public:
void readData();
void displayData();
ID: 22SCSE1012705
};
class Test : public Student {
protected:
float m1, m2, m3;
public:
void readMarks();
};
class Result : public Test {
private:
float tot, avg;
public:
void displayTotAvgGrades();
E2UC201C-2022-Section-43
};
#include "MultilevelInheritance2a.cpp"
int main() {
Result r;
r.readData();
r.readMarks();
r.displayData();
r.displayTotAvgGrades();
return 0;
}
Page No: 44
void Student :: displayData(){
cout<<"Id : "<<id<<endl;
cout<<"Name : "<<name<<endl;
}
void Test :: readMarks(){
ID: 22SCSE1012705
cout<<"Enter three subjects marks : ";
cin>> m1>>m2>>m3;
}
void Result :: displayTotAvgGrades(){
tot = m1+m2+m3;
avg = tot/3;
cout<< "Three subjects marks : "<<m1<<" "<<m2<<" "<<m3<<endl;
cout<<"Total marks : "<<tot<<endl;
cout<<"Average marks : "<<avg<<endl;
if(avg>=75)cout<<"Distinction"<<endl;
else if(avg >= 60) cout<<"First class"<<endl;
else if(avg>=50) cout<<"Second class"<<endl;
E2UC201C-2022-Section-43
else if(avg>=35) cout<<"Third class"<<endl;
else if(avg<35) cout<<"Very poor in studies"<<endl;
else cout<<"some error in avg";
}
Test Case - 2
User Output
Enter student id and name :
104 Vishnu
Enter three subjects marks :
78 98 85
Id : 104
Name : Vishnu
Three subjects marks : 78 98 85
Total marks : 261
Average marks : 87
Distinction
Page No: 45
ID: 22SCSE1012705
E2UC201C-2022-Section-43
Galgotias University Greater Noida
S.No: 25 Exp. Name: Program on Single Inheritance Date: 2023-03-06
Aim:
Write a C++ program to define a base class Student contains student id and name, a derived class
Page No: 46
Marks contains marks of 3 subjects, finally find a total and average marks of a student.
At the time of execution, the program should print the message on the console as:
ID: 22SCSE1012705
For example, if the user gives the input as:
Next, the program should print the message on the console as:
E2UC201C-2022-Section-43
then the program should print the result as:
Id : 2
Name : Gayle
Three subjects marks : 76.56 87.63 93.45
Total marks : 257.64
Average marks : 85.88
SingleInheritance2.cpp
// Uneditable driver code
#include <iostream>
using namespace std;
class Student {
private:
Page No: 47
int id;
char name[30];
public:
void readData();
void displayData();
ID: 22SCSE1012705
};
class Marks : public Student {
private:
float m1, m2, m3, total, avg;
public:
void readMarks();
void displayTotAvgMarks();
};
#include "SingleInheritance2a.cpp"
SingleInheritance2a.cpp
E2UC201C-2022-Section-43
// Type your code here to complete the functionality
void Student :: readData(){
cout<<"Enter student id and name : ";
cin>> id>> name;
}
void Student :: displayData(){
cout<<"Id : "<<id<<endl;
cout<<"Name : "<<name<<endl;
}
void Marks :: readMarks(){
Page No: 48
Id : 1
Name : Smith
Three subjects marks : 56 76 48
Total marks : 180
ID: 22SCSE1012705
Average marks : 60
Test Case - 2
User Output
Enter student id and name :
2 Gayle
Enter three subjects marks :
76.56 87.63 93.45
Id : 2
E2UC201C-2022-Section-43
Name : Gayle
Three subjects marks : 76.56 87.63 93.45
Total marks : 257.64
Average marks : 85.88
Page No: 49
Aim:
Use friend class: Develop a C++ program to find the area of a rectangle by converting the member of a
class square which is a friend class of rectangles. Declare Rectangle as a friend of Square so that Rectangle
member functions could have access to the private member of the square.
ID: 22SCSE1012705
Source Code:
rectangle.cpp
#include <iostream>
using namespace std;
class square{
private:
int side;
public:
int area(){
E2UC201C-2022-Section-43
return side*side;
}
friend class rectangle;
};
class rectangle{
public:
void setSide(square& x){
int side;
cin>>side;
x.side = side;
User Output
2
Area of rectangle: 4
10
User Output