0% found this document useful (0 votes)
95 views

C++ Lab Manual

This document contains 10 C++ programs demonstrating various programming concepts: 1. A program to test arithmetic operators on two user-input numbers. 2. A program that swaps the values of two user-input numbers. 3. A program that uses a switch statement to print the name of the month corresponding to a user-input month number. 4. A program that finds the roots of a quadratic equation given coefficients a, b, and c. 5. A program that checks if a user-input number is a palindrome. 6. A program that converts a binary number to its decimal equivalent. 7. A program that prints a triangle of numbers. 8. A program that searches

Uploaded by

mey chu
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)
95 views

C++ Lab Manual

This document contains 10 C++ programs demonstrating various programming concepts: 1. A program to test arithmetic operators on two user-input numbers. 2. A program that swaps the values of two user-input numbers. 3. A program that uses a switch statement to print the name of the month corresponding to a user-input month number. 4. A program that finds the roots of a quadratic equation given coefficients a, b, and c. 5. A program that checks if a user-input number is a palindrome. 6. A program that converts a binary number to its decimal equivalent. 7. A program that prints a triangle of numbers. 8. A program that searches

Uploaded by

mey chu
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/ 44

OBJECT ORIENTED

PROGRAMMING IN C++
LAB MANUAL

38 5
1. Program to test arithmetic operators.

Aim:- c++ program to demonstrate use of arithmetic operators.

Program:-

#include<iostream.h>
#include<conio.h>
Void main()
{
int num1,num2;
clrscr();
cout<<”Enter two numbers to be operated with arithmetic operators:”;
cin>>num1>>num2;
cout<<endl;
cout<<”Num1+Num2=”<<num1+num2<<endl;
cout<<”Num1*Num2=”<<num1*num2<<endl;
cout<<”Num1-Num2=”<<num1-num2<<endl;
if(num2!=0)
cout<<”Num1/Num2=”<<num1/num2<<endl;
else
cout<<”num2 is not non-zer. Division is not defined.”<<endl;
getch();
}

Output

Enter two numbers to be operated with arithmetic operators:


4
6
Num1+Num2= 10
Num1*Num2= 24
Num1-Num2= -2
Num1/Num2= 0

38 5
2. Program to swap two numbers.

Aim: Interchanging the values from a and b.

Program:

#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,temp;
clrscr();
cout<<"enter two number to swap";
cin>>a>>b;
cout<<"\n Before swapping"<<endl;
cout<<"\n a is="<<a<<"\n b is="<<b<<endl;
temp=a;
a=b;
b=temp;
cout<<"\n After swapping"<<endl;
cout<<"\n a is="<<a<<"\n b is="<<b<<endl;
getch();
}

Output

Enter two numbers to swap


4
5
Before swapping
A is = 4
B is = 5
After swapping
A is = 5
B is = 4

38 5
3. Program to demonstrate switch statement.

Aim: Print the month name using switch statement


Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int month;
clrscr();
cout<<"Enter month number";
cin>>month;
switch(month)
{
case 1: cout<<"month name = january";
break;
case 2: cout<<"month name = february";
break;
case 3: cout<<"month name = march";
break;
case 4: cout<<"month name = april";
break;
case 5: cout<<"month name = may";
break;
case 6: cout<<"month name = june";
break;
case 7: cout<<"month name = july";
break;
case 8: cout<<"month name = august";
break;
case 9: cout<<"month name = september";
break;
case 10: cout<<"month name = october";
break;
case 11: cout<<"month name = november";
break;
case 12: cout<<"month name = december";
break;
default:
cout<<"Please enter correct month number between 1 to 12";
break;

getch();
}

38 5
Output
Enter month number 12
Monthname = December

Enter month number 13


Please enter correct month number between 1 to 12

38 5
4. Program to find roots of quadratic equations.

Aim:- Find roots of quadratic equations.


Program:
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,x1,x2,determinant,realpart, imaginarypart;
clrscr();
cout<<"Enter coefficients a,b and c";
cin>>a>>b>>c;
determinant=b*b-4*a*c;
if(determinant>0)
{
x1=(-b+sqrt(determinant))/(2*a);
x2=(-b-sqrt(determinant))/(2*a);
cout<<"Roots are real and different."<<endl;
cout<<"x1="<<x1<<endl;
cout<<"x2="<<x2<<endl;
}
else if(determinant==0)
{
cout<<"Roots are real and same."<<endl;
x1=(-b+sqrt(determinant))/(2*a);
cout<<"x1=x2="<<x1<<endl;
}
else
{
realpart=-b/(2*a);
imaginarypart=sqrt(-determinant)/(2*a);
cout<<"Roots are complex and different"<<endl;
cout<<"x1="<<realpart<<"+"<<imaginarypart<<"i"<<endl;
cout<<"x2="<<realpart<<"-"<<imaginarypart<<"i"<<endl;
}
getch();
}
Output
Enter coefficients a,b and c
4
5
1
Roots are real and different
X1= -0.25
X2= -1

38 5
5. Program to check whether the given number is palindrome or not

Aim: To check whether a given number is palindrome or not.


Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int n,num,digit,rev=0;
clrscr();
cout<<"Enter a positive number";
cin>>num;
n=num;
do
{
digit=num%10;
rev=(rev*10)+digit;
num=num/10;
} while(num!=0);
cout<<"\n The reverse number is "<<rev<<endl;
if(n==rev)
cout<<"\n the number is a palindrome";
else
cout<<"\n the number is not a palindrome";
getch();
}

Output

Enter a positive number


12321
The reverse number is 12321
The number is a palindrome
Enter a positive number
12331
The reverse number is 13321
The number is not a palindrome

38 5
6. Program to convert binary number to decimal number.

Aim:- convert binary number to decimal number.


Program:
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n,num=0,d;
clrscr();
cout<<"enter any Binary number";
cin>>n;
cout<<"\n The decimal conversion of"<<n<<" is";
for(i=0;n!=0;++i)
{
d=n%10;
num=(d)*(pow(2,i))+num;
n=n/10;
}
cout<<"\t"<<num;
getch();
}

Output

Enter any Binary number


111
The decimal conversion of 111 is 7

38 5
7. Program to print the following format.

1
2 3
4 5 6
7 8 9 10

Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int rows,i,j,r,k=0;
clrscr();
cout<<"enter the number of rows";
cin>>rows;
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;++j)
cout<<k+j<<" ";
++k;
cout<<endl;
}
getch();
}

Output

1
2 3
4 5 6
7 8 9 10

38 5
8. Program to search an element in a given list.

Aim:- Searching an element in a given list.


Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int a[30],ele,num,i;
clrscr();
cout<<"Enter no of elements:";
cin>>num;
cout<<"\n Enter the values";
for(i=0;i<num;i++)
{
cin>>a[i];
}
cout<<"\n Enter element to be searched";
cin>>ele;
i=0;
while(i<num && ele!=a[i])
{
i++;
}
if(i<num)
{
cout<<"Number found at the location \t "<<i+1;
}
else
{
cout<<"Number not found";
}
getch();
}

Output

Enter no of elements 5
11 22 33 44 55
Enter the elements to be searched 44
Number found at the location 4

38 5
9. Program to perform addition of two matrices.

Aim:- To add two matrices.


Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int r,c,a[10][10],b[10][10],sum[10][10],i,j;
clrscr();
cout<<"Enter number of rows(between 1 and 10):";
cin>>r;
cout<<"Enter number of colums(between 1 and 10)";
cin>>c;
cout<<endl<<"Enter elements of 1st matrix:\n";
/* storing elements of first matrix entered by user.*/
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
cout<<" Enter element a"<<i+1<<j+1<<":";
cin>>a[i][j];
}
/* storing elements of second matrix entered by user.*/
cout<<"Enter elements of 2nd matrix \n";
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
cout<<" Enter element b"<<i+1<<j+1<<":";
cin>>b[i][j];
}
/* adding two matrices*/
for(i=0;i<r;i++)
for(j=0;j<c;j++)
sum[i][j]=a[i][j]+b[i][j];
//Displaying the result
cout<<endl<<"sum of two matrix is"<<endl;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
cout<<sum[i][j]<<" ";
if(j==c-1)
cout<<endl;
}
getch();
}

38 5
Output

Enter number of row(between 1 and 10) : 2


Enter number of columns(between 1 and 10): 2
Enter elements of 1st matrix
Enter elements a11: -4
Enter element a12: 5
Enter element a21: 6
Enter element a22: 8
Enter elements of 2nd matrix
Enter element b11: 3
Enter element b12: -9
Enter element b21: 7
Enter element b22: 2
Sum of two matrix is:
-1 -4
13 6

38 5
10. Program to perform multiplication of two matrices.

Aim:- To multiply the two matrices.


Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int r1,c1,r2,c2,a[10][10],b[10][10],mult[10][10],i,j,k;
clrscr();
cout<<"Enter number of rows and columns for 1st matrix:";
cin>>r1>>c1;
cout<<"Enter number of rows and colums for 2nd matrix";
cin>>r2>>c2;
cout<<"Enter elements of 1st matrix:\n";
/* storing elements of first matrix entered by user.*/
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
{
cout<<" Enter element a"<<i+1<<j+1<<":";
cin>>a[i][j];
}
/* storing elements of second matrix entered by user.*/
cout<<"Enter elements of 2nd matrix \n";
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
{
cout<<" Enter element b"<<i+1<<j+1<<":";
cin>>b[i][j];
}
//Initializing elements of matrix mul to 0.
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
{
mult[i][j]=0;
}
/* Multiplying matrix a and b and storing in array mult*/
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
for(k=0;k<c1;k++)

mult[i][j]=a[i][k]*b[k][j];
//Displaying the result
cout<<"Multiplication of two matrix is"<<endl;
for(i=0;i<r1;i++)

38 5
for(j=0;j<c2;j++)
{
cout<<mult[i][j]<<" ";
if(j==c2-1)
cout<<endl;
}
getch();
}

OUTPUT

Enter rows and columns for 1st matrix : 2 2


Enter rows and columns for 2nd matrix: 2 2
Enter elements of 1st matrix:
Enter element a11:1
Enter element a12:2
Enter element a21:3
Enter element a22:4
Enter elements of 2nd matrix:
Enter element b11:1
Enter element b12: 2
Enter element b21: 3
Enter element b22:4
Multiplication of two matrix is
6 8
12 16

38 5
11. Program to find factorial of a given number using recursion.

Aim:- To find the factorial of a given number using recursion.


Program:
#include<iostream.h>
#include<conio.h>
int factorial(int n);
void main()
{
int n;
clrscr();
cout<<"enter a positive number";
cin>>n;
cout<<"factorial of "<<n<<"=\t"<<factorial(n);
getch();
}
int factorial(int n)
{
if(n!=1)
return n*factorial(n-1);
}

Output

Enter a positive number


6
Factorial of 6 is = 720

38 5
12. Program to demonstrate pointer arithmetic.

Aim:- To perform an Arithmetic operators on pointers.


Program:
#include<iostream.h>
#include<conio.h>
int padd();
int pdiff();
void main()
{
int number;
clrscr();
cout<<"enter number 1 or 2";
cin>>number;
switch(number)
{
case 1: cout<<"\n addition of pointer";
int *ptr=(int*)5000;
ptr=ptr+5;
cout<<"\n New value of ptr:%u"<<ptr;
break;
case 2: cout<<"\n subtractio of pointers";
int *ptr1=(int*)5000;
ptr1=ptr1-3;
cout<<"\n new value of ptr %u:"<<ptr1;
break;
default:
cout<<"\n invalid";
break;
}
getch();
}

Output

Enter number 1 or 2
1
Addition of pointer
New value of ptr : 0x8f731392

38 5
13. Program to demonstrate call by value, call by address and call by reference.

Program:
// Using call by value.
#include<iostream.h>
#include<conio.h>
void interchange(int number1,int number2);
void main()
{
int num1=50,num2=70;
clrscr();
interchange(num1,num2);
cout<<"\n Number1:"<<num1<<"\n Number2:"<<num2;
getch();
}
void interchange(int number1,int number2)
{
int temp;
temp=number1;
number1=number2;
number2=temp;
}

Output

Number1=50
Number2=70

38 5
// Using call by reference
Program:
#include<iostream.h>
#include<conio.h>
void interchange(int *num1,int *num2);
void main()
{
int num1=50,num2=70;
clrscr();
interchange(&num1,&num2);
cout<<"\n Number1:"<<num1<<"\n Number2:"<<num2;
getch();
}
void interchange(int *num1,int *num2)
{
int temp;
temp=*num1;
*num1=*num2;
*num2=temp;
}

Output

Number1: 70
Number2: 50

38 5
14. Program to demonstrate structure data type.

Program:
#include<iostream.h>
#include<conio.h>
struct Person
{
char name[50];
int age;
float salary;
};
void main()
{
Person p1;
clrscr();
cout<<"enter full name";
cin.get(p1.name,50);
cout<<"\n enter age";
cin>>p1.age;
cout<<"\n enter salary";
cin>>p1.salary;
cout<<"\n Displaying Information"<<endl;
cout<<"\n Name:="<<p1.name;
cout<<"\n Age:="<<p1.age;
cout<<"\n Salary:="<<p1.salary;
getch();
}

Output

Enter full name : Sridhar


Enter age: 35
Enter salary : 10000
Displaying Information
Name:= Sridhar
Age:= 35
Salary:= 10000

38 5
15. Program to demonstrate enumeration.

Program:
#include<iostream.h>
#include<conio.h>
Enum Seasons {spring, summer, autumn, winter};
Void main()
{
Seasons S;
S=spring;
Cout<<”spring;=”<<s;
S=summer;
Cout<<”summer:=”<<s;
S=autumn;
Cout<<”autumn:=”<<s;
S=winter;
Cout<<”winter:=”<<s;
Getch();
}

Output

Spring = 0
Summer= 1
Autumn = 2
Winter = 3

38 5
10. Program to perform multiplication of two matrices.

Aim:- To multiply the two matrices.


Program:
#include<iostream.h>
#include<conio.h>
void main()
{
int r1,c1,r2,c2,a[10][10],b[10][10],mult[10][10],i,j,k;
clrscr();
cout<<"Enter number of rows and columns for 1st matrix:";
cin>>r1>>c1;
cout<<"Enter number of rows and colums for 2nd matrix";
cin>>r2>>c2;
cout<<"Enter elements of 1st matrix:\n";
/* storing elements of first matrix entered by user.*/
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
{
cout<<" Enter element a"<<i+1<<j+1<<":";
cin>>a[i][j];
38 5
}
/* storing elements of second matrix entered by user.*/
cout<<"Enter elements of 2nd matrix \n";
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
{
cout<<" Enter element b"<<i+1<<j+1<<":";
cin>>b[i][j];
}
//Initializing elements of matrix mul to 0.
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
{
mult[i][j]=0;
}
/* Multiplying matrix a and b and storing in array mult*/
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
for(k=0;k<c1;k++)

mult[i][j]=a[i][k]*b[k][j];
//Displaying the result
cout<<"Multiplication of two matrix is"<<endl;
for(i=0;i<r1;i++)

38 5
for(j=0;j<c2;j++)
{
cout<<mult[i][j]<<" ";
if(j==c2-1)
cout<<endl;
}
getch();
}

OUTPUT

Enter rows and columns for 1st matrix : 2 2


Enter rows and columns for 2nd matrix: 2 2
Enter elements of 1st matrix:
Enter element a11:1
Enter element a12:2
Enter element a21:3
Enter element a22:4
Enter elements of 2nd matrix:
Enter element b11:1
Enter element b12: 2
Enter element b21: 3
Enter element b22:4
Multiplication of two matrix is
6 8
12 16

38 5
38 5

11. Program to find factorial of a given number using recursion.


Aim:- To find the factorial of a given number using recursion.
Program:
#include<iostream.h>
#include<conio.h>
int factorial(int n);
void main()
{
int n;
clrscr();
cout<<"enter a positive number";
cin>>n;
cout<<"factorial of "<<n<<"=\t"<<factorial(n);
getch();
}
int factorial(int n)
{
if(n!=1)
return n*factorial(n-1);
}

Output

Enter a positive number


6
Factorial of 6 is = 720

38 5
12. Program to demonstrate pointer arithmetic.

Aim:- To perform an Arithmetic operators on pointers.


Program:
#include<iostream.h>
#include<conio.h>
int padd();
int pdiff();
void main()
{
int number;
clrscr();
cout<<"enter number 1 or 2";
cin>>number;
switch(number)
{ 38 5
case 1: cout<<"\n addition of pointer";
int *ptr=(int*)5000;
ptr=ptr+5;
cout<<"\n New value of ptr:%u"<<ptr;
break;
case 2: cout<<"\n subtractio of pointers";
int *ptr1=(int*)5000;
ptr1=ptr1-3;
cout<<"\n new value of ptr %u:"<<ptr1;
break;
default:
cout<<"\n invalid";
break;
}
getch();
}

Output

Enter number 1 or 2
1
Addition of pointer
New value of ptr : 0x8f731392

38 5
13. Program to demonstrate call by value, call by address and call by reference.

Program:
// Using call by value.
#include<iostream.h>
#include<conio.h>
void interchange(int number1,int number2);
void main()
{
int num1=50,num2=70;
clrscr();
interchange(num1,num2);
cout<<"\n Number1:"<<num1<<"\n Number2:"<<num2;
getch();
}
void interchange(int number1,int number2)
{
int temp;
temp=number1;
number1=number2;
number2=temp;
}

Output

Number1=50
Number2=70

38 5
38 5
// Using call by reference
Program:
#include<iostream.h>
#include<conio.h>
void interchange(int *num1,int *num2);
void main()
{
int num1=50,num2=70;
clrscr();
interchange(&num1,&num2);
cout<<"\n Number1:"<<num1<<"\n Number2:"<<num2;
getch();
}
void interchange(int *num1,int *num2)
{
int temp;
temp=*num1;
*num1=*num2;
*num2=temp;
}

Output

Number1: 70
Number2: 50

38 5
14. Program to demonstrate structure data type.

Program:
#include<iostream.h>
#include<conio.h>
struct Person
{
char name[50];
int age;
float salary; 38 5
};
void main()
{
Person p1;
clrscr();
cout<<"enter full name";
cin.get(p1.name,50);
cout<<"\n enter age";
cin>>p1.age;
cout<<"\n enter salary";
cin>>p1.salary;
cout<<"\n Displaying Information"<<endl;
cout<<"\n Name:="<<p1.name;
cout<<"\n Age:="<<p1.age;
cout<<"\n Salary:="<<p1.salary;
getch();
}

Output

Enter full name : Sridhar


Enter age: 35
Enter salary : 10000
Displaying Information
Name:= Sridhar
Age:= 35
Salary:= 10000

38 5
15. Program to demonstrate enumeration.

Program:
#include<iostream.h>
#include<conio.h>
Enum Seasons {spring, summer, autumn, winter};
Void main()
{
Seasons S;
S=spring;
Cout<<”spring;=”<<s;
S=summer;
Cout<<”summer:=”<<s;
S=autumn;
Cout<<”autumn:=”<<s;
S=winter;
Cout<<”winter:=”<<s;
Getch();
}

Output

Spring = 0
Summer= 1 38 5
Autumn = 2
Winter = 3
Winter 3

38 5
16. Program to demonstrate inline functions.

Program:
#include<iostream.h>
#include<conio.h>
class line
{
public:
inline float mul(float x, float y)
{
return(x*y);
}
inline float cube(float x)
{
return(x*x*x);
}
};
void main()
{
line obj;
float val1,val2;
clrscr();
cout<<"Enter two values";
cin>>val1>>val2;
cout<<"\n Multiplication value is:"<<obj.mul(val1,val2);
cout<<"\n cube value is:"<<obj.cube(val1)<<"\t"<<obj.cube(val2);
getch();
}

Output

Enter two values


5 7
Multiplication value is: 35

Cube value is: 125 343


38 5
17. Program to demonstrate function overloading.

Program:
#include<iostream.h>
38 5
#include<conio.h>
#include<stdlib.h>
#define pi 3.14
class fn
{
public:
void area(int); //circle
void area(int,int); //rectangle
void area(float,int,int); //triangle
};
void fn::area(int a)
{
cout<<"Area of circle"<<pi*a*a;
}
void fn::area(int a, int b)
{
cout<<"Area of rectangle "<<a*b;
}
void fn::area(float t,int a , int b)
{
cout<<"Area of triangle"<<t*a*b;
}
void main()
{
int ch,a,b,r;
clrscr();
fn obj;
cout<<"\n function overloading";
cout<<"\n 1. Area of circle \n 2. Area of rectangle \n 3. Area of triangle\n 4.exit";
cout<<"\n Enter your choice";
cin>>ch;
switch(ch)
{
case 1: cout<<"enter radious of the circle";
cin>>r;
obj.area(r);
break;
case 2: cout<<"enter sides of the rectangle";
cin>>a>>b;
obj.area(a,b);
break;
case 3: cout<<"enter sides of the triangle";

38 5
cin>>a>>b;
obj.area(0.5,a,b);
break;
case 4: exit(0);
}
getch();
}

Output

Function overloading
1.Area of circle
2. Area of rectangle
3. Area of triangle
4.exit
Enter your choice
2
Enter sides of the rectangle 38 5
5 5
Area of rectangle is : 25
Area of rectangle is : 25

38 5
18. Program to demonstrate class content.

Program:
#include<iostream.h>
#include<conio.h>
class stud
{
public:
char name[30],cname[10];
int rol,age;
void enter()
{
cout<<"enter student name:";
cin>>name;
cout<<"enter student age:";
cin>>age;
cout<<"enter student roll number";
cin>>rol;
cout<<"enter student student course";
cin>>cname;
}
void display()
{
cout<<"\n Age \t Name \t R.no \t course";
cout<<"\n"<<age<<"\t"<<name<<"\t"<<rol<<"\t"<<cname;
}
};
void main()
{
stud s;
clrscr();
s.enter();
s.display();
38 5
getch();
}
29. Program for Multiple and Hybrid Inheritance.

Program:
#include<iostream.h>
#include<conio.h>
class Stu
{
protected:
int rno;
public:
void get_no(int a)
{
rno=a;
}
void put_no(void)
{
cout<<"Roll no"<<rno<<"\n";
}
};
class test:public Stu
{
protected:
float part1,part2;
public:
void get_marks(float x,float y)
{
part1=x;
part2=y;
}
void put_marks()
{
cout<<"Marks obtained:"<<"part1="<<part1<<"\n"<<"part2="<<part2<<"\n";
}
};
class sports
{
protected:
float score;
public:
void getscore(float s)
{
score=s;
}
void putscore(void)
{
cout<<"sports"<<score<<"\n";

38 5
}
};
class result:public test,public sports
{
float total;
public:
void display(void);
};
void result::display(void)
{
total=part1+part2+score;
put_no();
put_marks();
putscore();
cout<<"Total score="<<total<<"\n";
}
void main()
{
result stu;
clrscr();
stu.get_no(123);
stu.get_marks(27.5,33.0);
stu.getscore(6.0);
stu.display();
getch();
}

Output

Roll no: 123


Marks obtained :
Part 1= 27.5
Part2=33
Sports=6
Total score= 66.5

38 5
30. Program to demonstrate pure virtual function implementation.

Program:
#include<iostream.h>
#include<conio.h>
class base
{
private:
int x;
float y;
public:
virtual void getdata();
virtual void display();
};
class der:public base
{
private:
int roll;
char name[20];
public:
void getdata();
void display();
};
void base::getdata()
{
}
void base::display()
{
}
void der::getdata()
{
cout<<"Enter roll of the student";
cin>>roll;
cout<<"Enter name of the student";
cin>>name;
}
void der::display()
{
cout<<"Name is:"<<name;
cout<<"\n Roll no is"<<roll;
}
void main()
{
base *ptr;
der obj;
clrscr();

38 5
ptr=&obj;
ptr->getdata();
ptr->display();
getch();
}

Output

Enter the roll no of the student; 111


Enter the name of the student: sachin
Name is; sachin
Roll no is: 111

38 5

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy