Вы находитесь на странице: 1из 18

Computer Programming

Assignment II

March 2019
Name:Simarinder Singh
UID:18BCS1958
Subject Code: CST-157
Branch/Sec/Group: CSE/10/B


CP ASSIGNMENT II !1
Qn.1 Write a program having a base class
‘Student’ with data member ‘roll_no’ and
member functions ‘getnum()’ to input roll_no
and ‘putnum()’ to display roll_no. A class
‘Test’ is derived from class ‘Student’ with
data member ‘marks’ and member functions
‘getmarks()’ to input marks and ‘putmarks()’
to display marks. Class ‘Sports’ is also
derived from class Student with data
member ‘score’ and member functions
‘getscore()’ to input score and ‘putscore()’to
display score. The class ‘Result’ is inherited
from two base classes, class Test and class
Sports with data member ‘total’ and a
member function ‘display()’ to display
rollno, marks, score and the total(marks +
score). 


Source code:

#include <iostream>

using namespace std;


class Student{
int roll_no;

CP ASSIGNMENT II !2
public:
void getnum(){
cout<<"Enter the Roll Number:";
cin>>roll_no;
}
void putnum(){
cout<<"Roll Number:"<<roll_no<<endl;
}
};
class Test:public virtual Students{
float marks;
public:
void getmarks(){
cout<<"Enter the Marks:";
cin>>marks;
}
void putmarks(){
cout<<"Marks:"<<marks<<endl;
}

};
class Sports:public virtual Students{
int score;
public:
void getscore(){
cout<<"Enter the Score:";

CP ASSIGNMENT II !3
cin>>score;
}
void putscore(){
cout<<"Score:"<<score<<endl;
}

};
class Result:public Test,public Sports{
float total;
public:
void display(){
total=marks+score;
cout<<“Total(Marks+Score):"<<total<<endl;
}

};

int main()
{
Result o;
o.getnum();
o.getmarks();
o.getscore();
o.putnum();
o.putmarks();

CP ASSIGNMENT II !4
o.putscore();
o.display();
return 0;
}
Output:
Enter the Roll Number:12345
Enter the Marks: 90
Enter the Score:10
Roll Number:12345
Marks: 90
Score:10
Total(Marks+Score):100

Qn.2 Write a program which uses catch(...)


handler.

Source Code:

#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter two Numbers: ";
cin>>a>>b;
try

CP ASSIGNMENT II !5
{
if(b==0)
throw b;
else if(b<0)
throw 'b';
else
throw "S";
}
catch(int n)
{
cout<<"Exception returned from the function: "<<n;
}
catch(char n)
{
cout<<n<<" is less than Zero";
}
catch(...)
{
cout<<"Default Exception";
}
return 0;
}

CP ASSIGNMENT II !6
Output:

Enter two Numbers:5


-5
-5 is less than Zero


Qn.3 What are nesting classes? Explain the


concept of nesting of classes with the help of
suitable programming example. 


A nested class is declared within the scope of another class. The


name of a nested class is local to its enclosing
class. Unless you use explicit pointers, references, or object names,
declarations in a nested class can only
use visible constructs, including type names, static members, and
enumerators from the enclosing class and global
variables.

Member functions of a nested class follow regular access rules and


have no special access privileges to members
of their enclosing classes. Member functions of the enclosing
class have no special access to members of a
nested class.

CP ASSIGNMENT II !7
Source Code:

#include<iostream>
using namespace std;
class A
{
public:
class B
{
private:
int num;
public:
void getdata(int n) {
num = n;
}
void putdata() {
cout<<"The number is "<<num;
}
};
};
int main()
{
cout<<"Nested classes in C++"<< endl;
A :: B obj;
obj.getdata(9);
obj.putdata();

CP ASSIGNMENT II !8
return 0;
}

Output:

Nested classes in C++


The number is 9

Qn.4 Differentiate among C++ multiple,


multilevel and hierarchical inheritance with
the help of suitable examples. 


Multilevel Inheritance:

Source Code:

#include <iostream>
using namespace std;
class base
{
public:
int x;
void getdata()
{
cout << “Enter the value of x= ";

CP ASSIGNMENT II !9
cin >> x;
}
};
class derive1 : public base
{
public:
int y;
void readdata()
{
cout << "Enter the value of y= "; cin >> y;
}
};
class derive2 : public derive1
{
private:
int z;
public:
void indata()
{
cout << "Enter the value of z= "; cin >> z;
}
void product()
{
cout << "Product= " << x * y * z;
}
};

CP ASSIGNMENT II !10
int main()
{
derive2 a;
a.getdata();
a.readdata();
a.indata();
a.product();
return 0;
}

Output:
Enter the value of x=5
Enter the value of y=10
Enter the value of z= 15
Product=750

Multiple Inheritance:

Source Code:

#include<iostream>
using namespace std;
class A
{
public:
int x;

CP ASSIGNMENT II !11
void getx()
{
cout << "Enter the value of x : "; cin >> x;
}
};
class B
{
public:
int y;
void gety()
{
cout << "Enter the value of y : "; cin >> y;
}
};
class C : public A, public B
{
public:
void sum()
{
cout << "Sum = " << x + y;
}
};
int main()
{
C obj1;
obj1.getx();

CP ASSIGNMENT II !12
obj1.gety();
obj1.sum();
return 0;
}

Output:
Enter the value of x : 25
Enter the value of y : 75
Sum = 100

Hierarchical Inheritance:

Source Code:

#include <iostream>
using namespace std;

class A
{
public:
int x, y;
void getdata()
{
cout << "\nEnter value of x and y:\n"; cin >> x >> y;
}
};

CP ASSIGNMENT II !13
class B : public A
{
public:
void product()
{
cout << "Product= " << x * y<<endl;
}
};
class C : public A
{
public:
void sum()
{
cout << "Sum= " << x + y<<endl;
}
};
int main()
{
B obj1;
C obj2;
obj1.getdata();
obj1.product();
obj2.getdata();
obj2.sum();
return 0;
}

CP ASSIGNMENT II !14
Output:
Enter value of x and y:
12
2
Product= 24
Enter value of x and y:
23
34
Sum= 57

Qn.5 A C++ Program that reads the marks


obtained of ten students out of 100. It also
computes the average; lowest and highest
marks. Then shows the difference of marks
of every student from the average marks.

Source code:

#include <iostream>
using namespace std;

class Student{
float marks[10],avg=0;

public:

CP ASSIGNMENT II !15
void getmarks(){
for(int i=0;i<10;i++){
cout<<"Enter the Marks:";
cin>>marks;
}
}
void average(){

for(int i=0;i<10;i++){
avg+=marks[i];
}
avg/=10;
cout<<"Average of Marks:"<<avg<<endl;
}
void high(){
float h=marks[0];
for(int i=1;i<10;i++){
if(h<marks[i])
h=marks[i];
}
cout<<"Highest Marks:"<<h<<endl;
}

void low(){
float l=marks[0];
for(int i=1;i<10;i++){

CP ASSIGNMENT II !16
if(h>marks[i])
l=marks[i];
}
cout<<"Lowest Marks:"<<l<<endl;
}
void diff(){
for(int i=0;i<10;i++){
cout<<"Difference for Student "<<i+1<<" : "<<avg-
marks[i]<<endl;
}
}

};

int main()
{
Student o;

o.getmarks();
o.average();
o.high();
o.low();
o.diff();
return 0;
}

CP ASSIGNMENT II !17
Output:
Enter the Marks:1
Enter the Marks:2
Enter the Marks:3
Enter the Marks:4
Enter the Marks:5
Enter the Marks:6
Enter the Marks:7
Enter the Marks:8
Enter the Marks:9
Enter the Marks:10
Average of Marks:5.5
Highest Marks:10
Lowest Marks:1
Difference for Student 1 : 4.5
Difference for Student 2 : 3.5
Difference for Student 3 : 2.5
Difference for Student 4 : 1.5
Difference for Student 5 : 0.5
Difference for Student 6 : -0.5
Difference for Student 7 : -1.5
Difference for Student 8 : -2.5
Difference for Student 9 : -3.5
Difference for Student 10 : -4.5

CP ASSIGNMENT II !18

Вам также может понравиться