KEMBAR78
CPP Lab Print | PDF | Namespace | Pointer (Computer Programming)
0% found this document useful (0 votes)
19 views48 pages

CPP Lab Print

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

CPP Lab Print

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

Exercise – 1(a)

AIM:
Write a Simple Program on printing “Hello World” and “Hello Name”
where name is the input from the user

Program:

#include <iostream>
using namespace std;
int main()
{
char name[10];
cout<<"Hello world\n";
cout<<"Enter your name:";
cin>>name;
cout<<"hello "<<name;
return 0;
}
Exercise – 1(b)
AIM:
Convert any two programs that are written in C into C++
i)C program for calculator:
Program:
#include<stdio.h>
main()
{
int a,b,ch;
printf("Enter two numbers");
scanf(“%d%d”,&a.&b);
printf("1.Addition\n2.Subtraction\n3.Multiplication\n4.Division");
printf("\nEnter your choice");
scanf(“%d”,&ch);
switch(ch)
{
case 1: printf("\nAddition is ");
printf(“%d”,(a+b)); break;
case 2: printf("\nSubtraction is”);
printf(“%d”,(a-b)); break;
case 3: printf("\nMultiplication is”);
printf(“%d”,(a*b)); break;
case 4: if(b!=0)
{
printf("\nDivision is ");
printf(“%d”,(a/b));
}
else

{ printf(“\n division not possible”); }


break;
default: cout<<"\nEnter your choice between 1 and 4";break;
}
return 0;
}
i)C++ program for calculator:

#include<iostream>
using namespace std;
int main()
{
int a,b,ch;
cout<<"Enter two numbers";
cin>>a>>b;
cout<<"1.Addition\n2.Subtraction\n3.Multiplication\n4.Division";
cout<<"\nEnter your choice";
cin>>ch;
switch(ch)
{
case 1: cout<<"Addition is ";
cout<<(a+b); break;
case 2: cout<<endl<<“Subtraction is”;
cout<<(a-b); break;
case 3: cout<<endl<<“Multiplication is”;
cout<<(a*b); break;
case 4: if(b!=0)
{
cout<<endl<<”Division is ";
cout<<(a/b);
}
else
{
printf(“\n division not possible”);
}
break;
default: cout<<"\nEnter your choice between 1 and 4";
break;
}
return 0;
}
ii)C program for verifying given number is palindrome or not
Program:
#include<stdio.h>
main()
{
int n,temp,x,rev=0;
printf("Enter a number");
scanf(“%d”,&n);
temp=n;
while(n>0)
{
x=n%10;
rev=rev*10+x;
n=n/10;

}
if(rev==temp)
printf("Given number is palindrome");
else
printf("Given number is not palindrome");
}
ii)C++ program for verifying given number is palindrome or not

Program:

#include<iostream>
using namespace std;
int main()
{
int n,temp,x,rev=0;
cout<<"Enter a number";
cin>>n;
temp=n;
while(n>0)
{
x=n%10;
rev=rev*10+x;
n=n/10;

}
if(rev==temp)
cout<<"Given number is palindrome";
else
cout<<"Given number is not palindrome";
return 0;
}
Exercise – 1(c)
AIM: Write a description of using g++

g++ is your friendly Gnu C++ compiler. g++ does not handle templates well, but
we can use them. g++ is known as a compiler, a program that will take your C++
source code and compile it into a binary file that can be executed to actually run
your program.

Compiling HelloWorld.cpp

Say you have a file helloworld.cpp as follows :

#include <iostream>
using namespace std;
int main ()
{
cout<<"Hello World”;
return 0;
}

You can compile and run it from the unix prompt as follows :
% g++ helloworld.cpp

Some options:

 -g - turn on debugging (so GDB gives more friendly output)


 -Wall - turns on most warnings
 -O or -O2 - turn on optimizations
 -o <name> - name of the output file
 -c - output an object file (.o)
 -I<include path> - specify an include directory
 -L<library path> - specify a lib directory
 -l<library> - link with library lib<library>.a
Exercise – 2(a):
AIM:Write a Program that computes the simple interest and compound
interest payable on Principle amount(in Rs.) of loan borrowed by the
customer from a bank for a given period of time (in years) at specific
rate of interest. Further determine whether the bank will benefit by
charging simple interest or compound interest.

Program:

#include <iostream>
#include<cmath>
using namespace std;
int main()
{
float p,r,t;
double CI,SI,x;
cout<<"Enter principle amount:";
cin>>p;
cout<<"Enter rate of interest:";
cin>>r;
cout<<"Enter time in years:";
cin>>t;
SI=(p*t*r)/100;
x=(1+r/100);
CI=p*(pow(x,t)-1);
cout<<"Simple Interest is:"<<SI;
cout<<"\nCompound Interest is:"<<CI;
if(CI>SI)
cout<<"\nCompound Interest is benefiter to bank:";
else
cout<<"Simple Interest is benefiter to bank:";

return 0;
}
Exercise – 2(b):
AIM: Write a Program to calculate the fare for the passengers
traveling in a bus. When a Passenger enters the bus, the conductor asks
“What distance will you travel?” On knowing distance from
passenger(as an approximate integer), the conductor mentions the fare
to the passenger according to following criteria: 0.64p per KM

Program:

#include<iostream>
using namespace std;
int main()
{
float dist,fare;
cout<<"Enter distance that passenger travels ";
cin>>dist;
fare=dist*0.64;
if(fare<5)
{
cout<<"passenger has to pay 5Rs";
}
else
{
cout<<"passenger has to pay:"<<fare;
}
return 0;
}
Exercise – 3(a):
AIM:Write a program to implement call by value and call by reference
using reference variable.

Program:
#include<iostream>
using namespace std;

// Parameters passed by value


void val_swap (int v1, int v2)
{
int temp;
temp = v1;
v1 = v2;
v2 = temp;
cout<<"After swapping: value1 = "<<v1<<" and value2 = "<<v2<<endl;
}

// Parameters are passed by reference


void ref_swap (int &r1, int &r2)
{
int temp;
temp = r1;
r1 = r2;
r2 = temp;
cout<<"After swapping: value1 = "<<r1<<" and value2 = "<<r2<<endl;
}

// Parameters passed by pointers


void ptr_swap (int *p1, int *p2)
{
int temp;
temp = *p1;
*p1 = *p2;
*p2 = temp;
cout<<"After swapping: value1 = "<<*p1<<" and value2 = "<<*p2<<endl;
}

int main ()
{
int var1 = 1, var2 = 2;
int var3 = 11, var4 = 22;
int var5 = 111, var6 = 222;

cout << "\n swap using pass by value :" <<endl;


cout << "Before swapping : value1 = " << var1 << ", value2 = " << var2 << endl;
val_swap(var1, var2);

cout << "\n swap using pass by references :" <<endl;


cout << "Before swapping : value1 = " << var3 << ", value2 = " << var4 << endl;
ref_swap(var3, var4);

cout << "\n swap using pass by pointers :" <<endl;


cout << "Before swapping : value1 = " << var5 << ", value2 = " << var6 << endl;
ptr_swap(&var5, &var6);

return(0);
}
Exercise – 3(b):
AIM:Write a program to illustrate scope resolution, new and delete
Operators. (Dynamic Memory Allocation)

Program:
#include<iostream>
using namespace std;
int m=10;
int main()
{ int m=20;
int *x=new int;
int *arr=new int(10);
cout<<"enter value into x";
cin>>*x;
cout<<"enter 10 elements into array";
for(int i=0;i<10;i++)
{
cin>>arr[i];
}
cout<<"local m="<<m<<"\n";
cout<<"global m="<<::m<<"\n";
cout<<"value of x="<<*x<<"\n";
cout<<"array elements are";
for(int i=0;i<10;i++)
{
cout<<" "<<arr[i];
}
return 0;
}
Exercise – 3(c):
AIM:Write a program to illustrate Storage classes

Program1:
#include<iostream>
using namespace std;
void staticv();
int main()
{
register int i,sum=0;
for(i=1;i<=10;i++)
{
sum=sum+i;
if(i==10) cout<<sum<<endl;
}
staticv();
staticv();
return 0;

}
extern int e=5;
void staticv()
{
static int s=0;
s++;
cout<<"s is"<<s<<endl;
cout<<"e is"<<e<<endl;
}
Program2:
File: sub.cpp

int test=100; // assigning value to test

void multiply(int n)
{
test=test*n;
}

File: main.cpp:

#include<iostream>
#include "sub.cpp" // includes the content of sub.cpp
using namespace std;
extern int test; // declaring test
int main()
{
cout<<test<<endl;
multiply(5);
cout<<test<<endl;
return 0;
}
Exercise – 3(d):
AIM:Write a program to illustrate Enumerations

Program:

#include<iostream>
using namespace std;

int main()
{
enum colors{red,blue,green=10,white,black,orange=22,pink,brown};
cout<<"red="<<red<<endl;
cout<<"blue="<<blue<<endl;
cout<<"green="<<green<<endl;
cout<<"white="<<white<<endl;
cout<<"black="<<black<<endl;
cout<<"orange="<<orange<<endl;
cout<<"pink="<<pink<<endl;
cout<<"brown="<<brown<<endl;
return 0;
}
Exercise –4(a) :

AIM:Write a program illustrating Inline Functions

Program:

#include<iostream>
using namespace std;
inline void sum(int a, int b);
inline void mul(int x,int y);

int main()
{
int m,n;
cout<<"enter 2 values";
cin>>m>>n;
sum(m,n);
mul(m,n);
return 0;
}
void sum(int a,int b)
{ cout<<"sum is"<<a+b<<endl;
}
void mul(int x,int y)
{ cout<<"multiplication is"<<x*y<<endl;
}
Exercise –4(b) :
AIM:Write a program illustrate function overloading. Write 2
overloading functions for power.

Program:
#include <iostream>
#include<cmath>
using namespace std;
class overload
{
public:
void power(int,int);
void power(double,int);
};
void overload::power(int b,int e)
{
cout<<"value of Power function is "<<pow(b,e)<<endl;
}
void overload::power(double b,int e)
{
cout<<"value of Power function is "<<pow(b,e)<<endl;
}

int main()
{
overload o1;
double b2;
int b1,e;
cout<<"Enter 3 nos:";
cin>>b1>>b2>>e;
o1.power(b1,e);
o1.power(b2,e);
return 0;
}
Exercise –4(c) :
AIM :Write a program illustrate the use of default arguments for
simple interest function.

Program:
#include <iostream>
using namespace std;
void simpleinterest(float p,float r=5.0,float t=6);
int main()
{ simpleinterest(15000);
simpleinterest(15000,3);
simpleinterest(15000,3,8);
return 0;
}
void simpleinterest(float p,float r,float t)
{
double SI;
SI=(p*t*r)/100;
cout<<"Simple Interest is:"<<SI<<endl;
}
Exercises –5(a) :
AIM: Write a program to illustrate function overloading. Write 2
overloading functions for adding two numbers.
Program:

#include<iostream>
using namespace std;
void addition(int x,int y);
void addition(float x,float y);
int main()
{
int a,b;
float c,d;
cout<<"enter a,b(integer) values";
cin>>a>>b;
cout<<"enter c,d(float) values";
cin>>c>>d;
addition(a,b);
addition(c,d);
return 0;
}
void addition(int x,int y)
{
cout<<"addition of 2 integer values is"<<x+y<<endl;
}
void addition(float x,float y)
{
cout<<"addition of 2 float values is"<<x+y<<endl;
}
Exercise -6 (Classes Objects)
AIM:Create a Distance class with:
• feet and inches as data members
• member function to input distance
• member function to output distance
• member function to add two distance objects
a)Write a main function to create objects of DISTANCE class. Input
two distances and output the sum.
Program:
#include<iostream>
using namespace std;
class dist
{
private:
int feet1,feet2,inch1,inch2,F,I;
public:
void read_distance()
{
cout<<"Enter first Distance:\n";
cout<<"Enter no of feet:";
cin>>feet1;
cout<<"Enter no of inches:";
cin>>inch1;
cout<<"Enter second Distance:\n";
cout<<"Enter no of feet:";
cin>>feet2;
cout<<"Enter no of inches:";
cin>>inch2;
}
void display_distance()
{
cout<<"First distance is:";
cout<<feet1<<"feet and "<<inch1<<"inches\n";
cout<<"Second distance is:";
cout<<feet2<<"feet and "<<inch2<<"inches\n";
}
void sum_distance()
{
F=feet1+feet2;
I=inch1+inch2;
if(I>=12)
{
F=F+1;
I=I-12;
}
cout<<"Total distance is:";
cout<<F<<"feet and "<<I<<"inches";
}

};
int main()
{
dist d1;
d1.read_distance();
d1.display_distance();
d1.sum_distance();
return 0;
}
Exercise -6 b
AIM: Write a C++ Program to illustrate the use of Constructors and
Destructors (use the above program.)
Program:
#include<iostream>
using namespace std;
class Distance
{
private:
int feet1,feet2,inch1,inch2,F,I;
public:
dist()
{
cout<<"Enter first Distance:\n";
cout<<"Enter no of feet:";
cin>>feet1;
cout<<"Enter no of inches:";
cin>>inch1;
cout<<"Enter second Distance:\n";
cout<<"Enter no of feet:";
cin>>feet2;
cout<<"Enter no of inches:";
cin>>inch2;
}
void display_distance()
{
cout<<"First distance is:";
cout<<feet1<<"feet and "<<inch1<<"inches\n";
cout<<"Second distance is:";
cout<<feet2<<"feet and "<<inch2<<"inches\n";
}
void sum_distance()
{
F=feet1+feet2;
I=inch1+inch2;
if(I>=12)
{
F=F+1;
I=I-12;
}
cout<<"Total distance is:";
cout<<F<<"feet and "<<I<<"inches";
}

~dist()
{
cout<<"\n\nDestructor is executed";
cout<<"\nEnd of the program";

};
int main()
{
dist d1;
//d1.read_distance();
d1.display_distance();
d1.sum_distance();
return 0;
}
Exercise -6 c
AIM: Write a program for illustrating function overloading in adding
the distance between objects (use the above problem)
Program:
#include<iostream>
using namespace std;
class dist
{
private:
int f1,f2,i1,i2;
public:
void dist_overload(int a,int b,int c,int d)
{
f1=a;
i1=b;
f2=c;
i2=d;
cout<<"The first distance is"<<endl;

cout<<f1<<"feet"<<i1<<"inches"<<endl;

cout<<"The second distance is"<<endl;


cout<<f2<<"feet"<<i2<<"inches"<<endl;

}
void dist_overload()
{
int feet,inch;
feet=f1+f2;
inch=i1+i2;

if(inch>12)
{
feet=feet+1;
inch=inch-12;
}
cout<<"Total Distance is:"<<feet<<"feet"<<inch<<"inches";
}
};
int main()
{
int f1,f2,i1,i2;
dist d;
cout<<"Enter First Distance:"<<endl;
cout<<"Enter no of feet:";
cin>>f1;
cout<<"Enter no of inches:";
cin>>i1;
cout<<"Enter Second Distance:"<<endl;
cout<<"Enter no of feet:";
cin>>f2;
cout<<"Enter no of inches:";
cin>>i2;
d.dist_overload(f1,i1,f2,i2);
d.dist_overload();
return 0;
}
Exercise -6 d
AIM: Write a C++ program demonstrating a Bank Account with
necessary methods and variables?

Program:
#include<iostream>
using namespace std;
float balance=10000;
class bank
{
private:
float wamount,damount;
public:
void withdrawl();
void deposit();
void balanceenquiry();
};
void bank::withdrawl()
{
cout<<"enter howmuch amount want to withdraw";
cin>>wamount;
balance=balance-wamount;
cout<<"Balance after withdrawl is";
cout<<balance;
}
void bank::deposit()
{
cout<<"enter howmuch amount want to deposit";
cin>>damount;
balance=balance+damount;
cout<<"Balance after withdrawl is"<<endl;
cout<<balance<<endl;
}
void bank::balanceenquiry()
{
cout<<endl<<"account balance is"<<balance<<endl;
}
int main()
{
bank b;
b.balanceenquiry();
b.deposit();
b.withdrawl();
b.balanceenquiry();
return 0;
}
Exercise – 7
AIM: Write a program for illustrating Access Specifiers public, private,
protected.
Program:
#include<iostream>
using namespace std;
class sample
{
private:
int a;
public:
int b;
void read();
void show();
protected:
int c;
};
void sample::read()
{
cout<<"enter a,b,c values";
cin>>a>>b>>c;
}
void sample::show()
{
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
}
int main()
{
sample s;
s.read();
s.show();
s.b=20;//values of a and c are not accessible here
cout<<"after changing the value of b,values of a,b,c,are"<<endl;
s.show();
return 0;
}
Exercise – 7a
AIM:Write a program implementing Friend Function?
Program:
#include<iostream>
using namespace std;
class FRIEND
{
private:
int x,y,z;
public:
void read();
void display();
friend void show(FRIEND);

};
void show(FRIEND f1)
{
cout<<"\nDisplaying values using friend function:\n";
cout<<"x value is:"<<f1.x<<"\ny value is:"<<f1.y;
}

void FRIEND::read()
{
cout<<"Enter x and y values:";
cin>>x>>y;
}

void FRIEND::display()
{
cout<<"\nDisplaying values using member function:\n";
cout<<"x value is:"<<x<<"\ny value is:"<<y;
}

int main()
{
FRIEND f;
f.read();
show(f);
f.display();
return 0;
}
Exercise – 7b
AIM: Write a program to illustrate this pointer
Program:
#include<iostream>
using namespace std;
class A
{ int a;
public:
void read()
{

cout<<"enter a ";
cin>>a;
this->show();
}
void show()
{
cout<<this->a;
}
};
int main()
{
A ob;
ob.read();
ob.show();
return 0;
}
Exercise – 7c
AIM: Write a Program to illustrate pointer to a class
Program:
#include<iostream>
using namespace std;
class Pointer
{
public:
int x,y;
void arithmetic();
};
void Pointer::arithmetic()
{
cout<<"\n Addition is:"<<(x+y);
cout<<"\n Subtraction is:"<<x-y;
cout<<"\n Multiplication is:"<<x*y;
cout<<"\n Division is:"<<(float)x/y;
}
int main()
{
Pointer a,*p;
p=&a;
cout<<"Enter two numbers:";
cin>>p->x;
cin>>p->y;
p->arithmetic();
return 0;
}
Exercise -8a (Operator Overloading)
AIM:Write a program to Overload Unary Operator as Member
Function.
Program:
#include<iostream>
using namespace std;
class demo
{
int a,b;
public:
void read()
{
cout<<"enter a,b";
cin>>a>>b;
}
void operator-()
{
a=-a;
b=-b;
}
void show()
{
cout<<endl<<a<<" "<<b;}
};
int main()
{
demo d;
d.read();
d.show();
-d;
d.show();
return 0;
}
Exercise -8a (Operator Overloading)
AIM:Write a program to Binary Operators as Non Member Function.
Program:
#include<iostream>
using namespace std;
class demo
{
int a,b;
public:

void read()
{
cout<<"enter a,b";
cin>>a>>b;
}
friend demo operator+(demo d1,demo d2)
{
demo dd;
dd.a=d1.a+d2.a;
dd.b=d1.b+d2.b;
return dd;
}
void show()
{
cout<<endl<<"a="<<a<<" "<<"b="<<b<<endl;
}
};
int main()
{
demo d1,d2,d3;
cout<<"for d1 ";
d1.read();
cout<<"for d2 ";
d2.read();
d1.show();
d2.show();
d3=d1+d2;
cout<<"After adding two objects into d3"<<endl;
d3.show();
}
Exercise -8b
AIM:Write a c ++ program to implement the overloading assignment =
operator
Program:
#include<iostream>
using namespace std;
class assignment
{
int a;
public:

void read()
{
cout<<"enter a";
cin>>a;
}
void operator=(assignment obj)
{
a=obj.a;
}
void show()
{
cout<<endl<<"a="<<a<<endl;
}
};

int main()
{
assignment a1,a2;
cout<<"enter value for object a1 ";
a1.read();
cout<<"enter value for object a2 ";
a2.read();
cout<<" value of a for object a1 is ";
a1.show();
cout<<" value of a for object a2 is ";
a2.show();
cout<<"After overloading assignment operator"<<endl;
a2.operator=(a1);
cout<<" value of a for object a1 is ";
a1.show();
cout<<" value of a for object a2 is ";
a2.show();
return 0;
}
Exercise -8c

AIM:Write a case study on Overloading Operators and Overloading


Functions.

Description:
Function Overloading:
In C++, we have multiple definitions for the same function name in the same scope.
The definition of the function must differ from each other by the types and/or the
number of arguments in the argument list. he appropriate function will be identified
by the compiler by examining the number or the types of parameters / arguments in
the overloaded function. Function overloading reduces the investment of different
function names and used to perform similar functionality by more than one
function.we can not overload function declarations that differ only by return type.
Operator Overloading:
In C++, we can redefine or overload most of the built-in operators available in
C++. Thus a programmer can use operators with user-defined types as well. It is a
type of polymorphism in which an operator is overloaded to give user defined
meaning to it.
Overloaded operators are functions with special names the keyword operator
followed by the symbol for the operator being defined. Like any other function, an
overloaded operator has a return type and a parameter list.
Exercise -9a)i) (Inheritance)
AIM:Write C++ Program for Single Inheritance?

Program:
#include <iostream>
using namespace std;

// Base class
class shape
{
protected:
int width;
int height;
public:
void getwidth()
{
cout<<"enter width";
cin>>width;
}

void getheight()
{
cout<<"enter height";
cin>>height;
}
};

class rectangle: public shape


{
public:
void area()
{
cout<<(width*height);
}
};

int main()
{
rectangle r;

r.getwidth();
r.getheight();

// Print the area of the object.


cout << "Total area: " ;
r.area();
return 0;
}
Exercise -9a)ii)
AIM:Write C++ Program for Hierarchical Inheritance?
Program:
#include <iostream>

using namespace std;


class shape
{
protected:
float width, height;
public:
void getdata ()
{
cout<<"enter width and height";
cin>>width>>height;
}
};

class rectangle: public shape


{
public:
void area()
{
cout<<"area of rectangle is"<<(width * height)<<endl;
}
};

class triangle: public shape


{
public:
float area ()
{
cout<<"area of triangle is "<<(width * height / 2);
}
};
int main ()
{
rectangle r;
triangle t;
cout<<"Rectangle Data"<<endl;
r.getdata();
cout<<"Traingle Data"<<endl;
t.getdata();
r.area();
t.area();
return 0;
}
Exercise -9a)iii)
AIM:Write C++ Program for Multiple Inheritance?
Program:
#include <iostream>

using namespace std;

// Base class
class rectangle
{
protected:
int width;
int height;
public:
void getwidth()
{
cout<<"enter width of rectangle";
cin>>width;
}

void getheight()
{
cout<<"enter height of rectangle";
cin>>height;
}
};
class square
{
protected:
int a;

public:
void getlength()
{
cout<<"enter length of side of a square";
cin>>a;
}

};
class shape: public rectangle,public square
{
public:
void area()
{
cout<<"area of rectangle is"<<(width*height)<<endl;
cout<<"area of square is"<<(a*a)<<endl;
}
};

int main()
{
shape s;

s.getwidth();
s.getheight();
s.getlength();
// Print the area of the object.
s.area();

return 0;
}
Exercise -9a)iv)
AIM:Write C++ Program for Multilevel Inheritance?

Program:
#include<iostream>
using namespace std;
class student
{
protected:
int rollno;
public:
void getnum()
{
cout<<"enter roll number";
cin>>rollno;
}
void shownum()
{
cout << "Roll Number Is:\n"<< rollno << "\n";
}
};
class marks:public student
{
protected:
int sub1;
int sub2;
public:
void getmarks()
{
cout<<"enter marks for sub1,sub2";
cin>>sub1>>sub2;
}
void showmarks()
{
cout << "Subject 1:" << sub1 << "\n";
cout << "Subject 2:" << sub2 << "\n";
}
};
class result : public marks
{
protected:
float tot;
public:
void display()
{
tot = sub1+sub2;
shownum();
showmarks();
cout << "Total:"<< tot;
}
}; int main()
{
result r;
r.getnum();
r.getmarks();
r.display();
return 0;
}
Exercise -9a)v)
AIM:Write C++ Program for Hybrid Inheritance?
Program:
include<iostream>
using namespace std;
class student
{
protected:
int rollno;
public:
void getnum()
{
cout<<"enter roll number";
cin>>rollno;
}
void shownum()
{
cout << "Roll Number Is:\n"<< rollno << "\n";
}
};
class marks:public student
{
protected:
int sub1;
int sub2;
public:
void getmarks()
{
cout<<"enter marks for sub1,sub2";
cin>>sub1>>sub2;
}
void showmarks()
{
cout << "Subject 1:" << sub1 << "\n";
cout << "Subject 2:" << sub2 << "\n";
}
};
class sports
{
protected:
int score;
public:
void getscore()
{
cout<<"enter sports score";
cin>>score;
}
void showscore()
{
cout << endl<<"sports score is" <<score<<endl;
}
};
class result : public marks,public sports
{
protected:
float tot;
public:
void display()
{
tot = sub1+sub2;
shownum();
showmarks();
cout << "Total:"<< tot;
showscore();
}
}; int main()
{
result r;
r.getnum();
r.getmarks();
r.getscore();
r.display();
return 0;
}
Exercise -9b)
AIM:Write a program to show Virtual Base Class?
Program:
#include<iostream>
using namespace std;
class student
{
protected:
int rollno;
public:
void getnum()
{
cout<<"enter roll number";
cin>>rollno;
}
void shownum()
{
cout << "Roll Number Is:\n"<< rollno << "\n";
}
};
class marks:virtual public student
{
protected:
int sub1;
int sub2;
public:
void getmarks()
{
cout<<"enter marks for sub1,sub2";
cin>>sub1>>sub2;
}
void showmarks()
{
cout << "Subject 1:" << sub1 << "\n";
cout << "Subject 2:" << sub2 << "\n";
}
};
class sports:virtual public student
{
protected:
int score;
public:
void getscore()
{
cout<<"enter sports score";
cin>>score;
}
void showscore()
{
cout << endl<<"sports score is" <<score<<endl;
}
};
class result : public marks,public sports
{
protected:
float tot;
public:
void display()
{
tot = sub1+sub2;
shownum();
showmarks();
cout << "Total:"<< tot;
showscore();
}
}; int main()
{
result r;
r.getnum();
r.getmarks();
r.getscore();
r.display();
return 0;
}
Exercise -9c)
AIM:Write a case study on using virtual classes?
Virtual classes:
In object-oriented programming, a virtual class is a nested inner class whose
functions and member variables can be overridden and redefined by subclasses of
the outer class.Virtual classes are analogous to virtual functions.The run time type
of a virtual class depends on the run time type of an object of the outer class. (Just
like the run time type of an object decides which virtual function should be used.)
A run time instance type of the outer class object not only decides on the
polymorphic type of its own type object, but also on a whole family tree of virtual
class members. Virtual classes solve the extensibility problem of extending data
abstraction with new functions and representations. Like virtual functions, virtual
classes follow the same rules of definition, overriding, and reference.

When a derived class inherits from a base class, it must define or override the
virtual inner classes it inherited from the base class. An object of the child class
may be referred to by a reference or pointer of the parent class type or the child
class type. When the reference or pointer invoke the virtual inner classes, the
derived class's implementation will be called if the object is of the derived class
type. The type of the outer class determines the run time of the inner virtual class.

A method with an object argument has access to the object's virtual classes. The
method can use the virtual classes of its arguments to create instances and declare
variables. Virtual classes of different instances are not compatible.
Virtual base classes:
An ambiguity can arise when several paths exist to a class from the same base class.
This means that a child class could have duplicate sets of members inherited from a
single base class.C++ solves this issue by introducing a virtual base class. When a
class is made virtual, necessary care is taken so that the duplication is avoided
regardless of the number of paths that exist to the child class.
When two or more objects are derived from a common base class, we can prevent
multiple copies of the base class being present in an object derived from those
objects by declaring the base class as virtual when it is being inherited. Such a base
class is known as virtual base class. This can be achieved by preceding the base
class’ name with the word virtual.

You might also like