Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

C++ LAB RECORD

This document is a laboratory record for an OOPs with C++ programming course at Centurion University, detailing various programming exercises. It includes code examples for basic C programs, object-oriented programming, and class-object-constructor programs, along with their outputs. The record is submitted by a student and includes a certificate section for faculty validation.

Uploaded by

riksohom3
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

C++ LAB RECORD

This document is a laboratory record for an OOPs with C++ programming course at Centurion University, detailing various programming exercises. It includes code examples for basic C programs, object-oriented programming, and class-object-constructor programs, along with their outputs. The record is submitted by a student and includes a certificate section for faculty validation.

Uploaded by

riksohom3
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 81

CENTURION

UNIVERSITY OF TECHNOLOGY AND MANAGEMENT

SCHOOL OF ENGINEERING & TECHNOLOGY

PARALAKHEMUNDI

OOPs with C++ Programming


LABORATORY RECORD

Submitted By

Name Ayush Naman

Regd. No : 190301200004

Branch : Computer Science and Engineering Semester: 3rd

2020 – 2021
CENTURION UNIVERSITY OF TECHNOLOGY AND MANAGEMENT
SCHOOL OF ENGINEERING & TECHNOLOGY, PARALAKHEMUNDI/JATNI

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

CERTIFICATE

This is to certify that


Mr/Ms. Sunkari Krishnaveni
With Registration No.190101120070
Of B.Tech 3rd Semester has conducted ____
Experiments in
___________________________Laboratory

Faculty in -Charge Head of


the Department

Office Seal
BASIC C PROGRAMS:
1) Write a Program to perform
Parameter passing.
CODE:
#include<iostream>
using namespace std;
void fun(string s1,string s2){
cout << s1 << "\n" << s2;
}
int main(){
fun("CENTURION
UNIVERSITY","PARLAKHEMUNDI");
return 0;
}
OUTPUT:
2) Write a program to create a
scientific calculator.
CODE:
#include<iostream>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<iomanip>
char op;
using namespace std;
void sum()
{

int sum = 0;
int n;
int numberitems;
cout << "Enter number of items: \n";
cin >> numberitems;

for(int i=0;i<numberitems;i++)
{
cout<< "Enter number "<<i<<":\n\n" ;
cin>>n;
sum+=n;
}
cout<<"sum is: "<< sum<<endl<<endl;
}
void diff()
{
int diff;
int n1,n2;
cout<<"enter two numbers to find their
difference:\n\n";
cout<<"enter first number:";
cin>>n1;
cout<<"\nenter second number:";
cin>>n2;
diff=n1-n2;
cout<<"\ndifference
is:"<<diff<<endl<<endl;
}

void pro()

{
int pro=1;
int n;
int numberitems;
cout<<"enter number of items:\n";
cin>>numberitems;
for(int i=0;i<=numberitems;i++)
{
cout<<"\nenter item "<<i<<":";
cin>>n;
pro*=n;
}

cout<<"product
is:"<<pro<<endl<<endl;
}

void div()
{
int div;
int n1;
int n2;
cout<<"enter 2 numbers to find their
quotient\n\n";
cout<<"enter numerator:";
cin>>n1;
cout<<"\nenter denominator:";
cin>>n2;
div=n1/n2;
cout<<"\nquotient
is:"<<div<<endl<<endl;
}

void power()
{
long int p;
int res=1,n;
cout<<"enter number:";
cin>>n;
cout<<"\nenter power:";
cin>>p;
for(int i=1;i<=p;i++)
{
res=n*res;
}
cout<<n<<"\n power "<<p<<"
is :"<<res<<endl;
}

void sq()
{
float s;
int n;
cout<<"enter number to find its square
root:";
cin>>n;
s=sqrt(n);
cout<<"\nsquare root of "<<n<<"
is :"<<s<<endl;
}
void fact()
{
long int f=1;
int c=1,n;
cout<<"enter number to find its
factorial:";
cin>>n;
while(c<=n)
{
f=f*c;
c+=1;
}
cout<<"\nfactorial of "<<n<<"
is :"<<f<<endl;
}
void expo()
{
long double res=1,p;
double e=2.718281828;
cout<<"enter power of exponential
function:";
cin>>p;
for(int i=1;i<=p;i++)
{
res=e*res;
}
cout<<" e^ "<<p<<" is :"<<res<<endl;

}
int main()
{

system("cls");
do
{

system("pause");
system("cls");
cout<<"***which operation you want to
perform***\n";
cout<<"press 0 for exit\n";
cout<<"press 1 for addition \n";
cout<<"press 2 for subtraction\n";
cout<<"press 3 for multiplication\n";
cout<<"press 4 for division\n";
cout<<"press 5 for power calculation\n";
cout<<"press 6 for square root \n";
cout<<"press 7 for factorial calculation\
n";
cout<<"press 8 for exponential
calculation\n";
cout<<"press option:";
cin>>op;
switch(op)
{
case '1':
sum();

break;
case '2':
diff();
break;
case '3':
pro();
break;
case '4':
div();
break;
case '5':
power();
break;
case '6':
sq();
break;
case '7':
fact();
break;
case '8':
expo();
break;
case '0':
exit(0);
default:
cout<<"invalid input" ;
system("cls");
}
}
while(op!='0');

getch();
}
OUTPUT:
3) Write a program to convert a
decimal to binary number using
recursion.
CODE:
#include <iostream>
using namespace std;
int main(){
int s[128], num, i=0;
cout << "Enter a number to convert: ";
cin >> num;
while(num > 0){
s[i]=num%2;
num= num/2;
i++;
}
cout << "Binary of the number: ";
for(int j = i-1; j >= 0; j--){
cout << s[j];
}
cout << endl;
return 0;
}

OUTPUT:
4) Write a program to Read 'n'
employee details and display the
top 10 employees as per the salary.
CODE:
#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
struct emp{
char name[20];
int code;
float sal;
};

emp read()
{emp e;
cout << "Enter name: ";
cin >> e.name;
cout << "Enter code no: ";
cin >> e.code;
cout << "Enter salary: ";
cin >> e.sal;
return e;
}

void display(emp e)
{cout << "Name: " << e.name << endl;
cout << "Code no: " << e.code << endl;
cout << "Salary: " << e.sal << endl;
}

void sort(emp e[], int n)


{for(int i = 1; i < n; i++)
for(int j = 0; j < n - i; j++)
if(e[j].sal < e[j+1].sal)
{emp temp = e[j];
e[j] = e[j+1];
e[j+1] = temp;
}
}
int main()
{
emp e[100];
int i,n;

cout << "Enter no of emloyees: ";


cin >> n;
for(int i = 0; i < n; i++)
e[i] = read();
sort(e, n);
for(i = 0; i < n; i++)
display(e[i]);
}
OUTPUT:
5) Write a program to create a text file
and display it.
CODE:
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream file;
file.open ("learn.txt");
if (file.is_open())
{
file << "Hello everyone.\n";
file << "I am from CSE branch of 3rd
semester.\n";
file << "let us learn the programs of c+
+.\n";
file.close();
}

else cout << "cannot open a file";


return 0;
}

OUTPUT:

OBJECT-ORIENTED BASIC
PROGRAMS:
1. Write a program to read a number
and check whether the number is
Prime number, Palindrome number,
Magic number, Armstrong number,
Strong number or not.
CODE:
#include <iostream>
#include <math.h>
using namespace std;
int checkPrimeNumber(int num);
int checkArmstrongNumber(int
number);
int checkpalindromeno(int num);
int checkstrongno(int num);

int main(){

int num, flag;

cout<<"Enter a positive integer: ";


cin>>num;

// Checking prime number


flag =checkPrimeNumber(num);
if (flag == 1
)
cout<<num<<" is a prime
number"<<endl;
else
cout<<num<<" is not a prime
number"<<endl;

// Checking Armstrong number


flag = checkArmstrongNumber(num);
if (flag == 1)
cout<<num<<" is a Armstrong
number"<<endl;
else
cout<<num<<" is a not an
Armstrong number"<<endl;

flag =checkpalindromeno(num);
if (flag == 1)
cout<<num<<" is a palindrome
number"<<endl;
else
cout<<num<<" is not a palindrome
number"<<endl;

flag =checkstrongno(num);
if (flag == 1)
cout<<num<<" is a strong
number"<<endl;
else
cout<<num<<" is not a strong
number"<<endl;
return 0;
}
//function to check prime number
int checkPrimeNumber(int num)
{
int i, flag = 1;

for(i=2; i<=num/2; ++i)


{

// condition for non-prime number


if(num%i == 0)
{
flag = 0;
break;
}
}
return flag;
}

//function to check Armstrong Number


int checkArmstrongNumber(int number)
{
int originalNumber, remainder, result
= 0, num = 0, flag;

originalNumber = number;

while (originalNumber != 0)
{
originalNumber /= 10;
++num;
}

originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber%10;
result += pow(remainder, num);
originalNumber /= 10;
}

// condition for Armstrong number


if(result == number)
flag = 1;
else
flag = 0;

return flag;
}

int checkpalindromeno(int num)


{
int digit, rev = 0,n,flag;
n = num;

do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);

cout << " The reverse of the number


is: " << rev << endl;
if (n == rev)
flag=1;
else
flag=0;

return flag;
}
int checkstrongno(int num)
{
int num,sum=0,flag;
int save=num;
//logic to check for Strong Number
starts
while(num)
{
int n=num%10;
int fact = 1;
//finding factorial of each digit of
input
for(int i=num;i>0;i--)
{
fact=fact*i;
}
sum+=fact;
num/=10;
}
//checking for Strong Nunber
if(sum==save)
{
flag=1;
}
else
{
flag=0;
}
//logic ends
return flag;
}

OUTPUT:

2. Write definitions for two versions of


an overloaded function. This
function’s 1st version sum() takes
an argument, int array, and returns
the sum of all the elements of the
passed array. The 2nd version of
sum() takes two arguments, an int
array and a character (‘E’ or ‘O’).
If the passed character is ‘E’, it
returns the sum of even elements of
the passed array and is the passed
character is ‘O’, it returns the sum
of odd elements. In case of any
other character, it returns 0 (zero).
CODE:
#include<iostream>
using namespace std;
int sum(int arr[],int n)
{
int sum=0,i;
for(i=0;i<n;i++)
{
sum=sum+arr[i];
}
cout<<"Sum of all the elements in
array : "<<sum<<endl;

}
int sum(char ch,int arr[],int n)
{
int i;
int sum=0;

if((ch=='E')||(ch=='e'))
{
for(i=0;i<n;i=i+1)
{
if(arr[i]%2==0)
{
sum=sum+arr[i];
}
}

}
else if((ch=='O')||(ch=='o'))
{
for(i=0;i<n;i=i+1)
{
if(arr[i]%2==1)
{
sum=sum+arr[i];

}
}

cout<<"Sum of your desired elements


: "<<sum;
}

int main()
{

int n,i;
int arr[60];
char ch;
cout<<"How many numbers you
want in your array : ";
cin>>n;
cout <<"\nEnter the numbers : ";
for(i=0;i<n;i++)
{
cin>>arr[i];
}
cout <<"\nIf you want the sum of
even numbers of the array Enter E or
you want the sum of odd numbers enter
O\n";
cout <<"Enter the Chracter :";
cin>>ch;
sum(arr,n);
sum(ch,arr,n);

}
OUTPUT:
CLASS-OBJECT-CONSTRUCTOR
programs:
1. Define a class to represent a book
in a library. Include the following
members:
Data Members
Book Number, Book Name, Author,
Publisher, Price, No. of copies
issued, No. of copies
Member Functions
(i) To assign initial values
(ii) To issue a book after checking
for its availability
(iii) To return a book
(iv) To display book information.
CODE:
#include<iostream>
using namespace std;
class library
{
private:
int book_num;
char book_name[23];
char author[25];
char publisher[20];
float price;
int No_of_copies_issued;
int No_of_copies;
public:
void assign()
{
cout<<"Enter Book number,Book
name,author of book,publisher of
book,price\n and No.of copies are
available of the book";

cin>>book_num>>book_name>>author>
>publisher>>price>>No_of_copies;
}
void issue_book()
{
cout<<"\nEnter book details..";
assign();
if(No_of_copies>0)
{
cout<<"\nEnter No_of_copies of you
want";
cin>>No_of_copies_issued;

if(No_of_copies>=No_of_copies_issued)
{
No_of_copies=No_of_copies-
No_of_copies_issued;
cout<<"\nBooks issued"<<"
Submit the book within time";
}
else
{
cout<<"\ncopies
issued"<<"Book is not available in
stock..";
}
}
else
{
cout<<"book is not
available";
}
}

void return_book()
{
cout<<"\nEnter book details
which you want to return";
cout<<"\nEnter book
number,book name and no of copies
taken";

cin>>book_num>>book_name>>No_of_co
pies_issued;

No_of_copies=No_of_copies+No_of_copies_
issued;
cout<<"\nBook is returned";
}
void display()
{
cout<<"\nBook
Number:"<<book_num<<"\nBook
Name:"<<book_name<<"\nAuthor of
book:"<<author<<"\nPublisher of
book:"<<publisher<<"\nPrice of
book:"<<price;

}
};
int main()
{
library l;
int ch;
cout<<"\nWelcome to Centurian
library";
cout<<"\nPress [1]-issue book\n\t[2]-
Return book";
cout<<"\nEnter your choice";
cin>>ch;
switch(ch)
{
case 1:
{
l.issue_book();
break;
}
case 2:
{
l.return_book();
break;
}
}

OUTPUT:
2. A bank maintains two kinds of
accounts for customers, one called
as savings and the other as
current account. The savings
account provides compound
interest and withdrawal facilities
but no cheque book facility. The
current account provides cheque
book facility but no interest.
Current account holders should
also maintain a minimum balance
and if the balance falls below this
level a service charge is imposed.
Define a class to represent a bank
account. Include the following
members: Data members: 1. Name
of the depositor. 2. Account
number. 3. Type of account. 4.
Balance amount in the account.
Member functions: 1. To assign
initial values. 2. To deposit an
amount. 3. To withdraw an amount
after checking the balance. 4. To
display the name and balance.
Write a main program to test the
program

CODE:
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
//class account declaration and definition
class account
{
public:
char name[30];
long int acc_num;
int acc_type;
int balance;
int amount;

//to initialize instance members


void getData()
{
cout<<"\nEnter the following details\
nCustomer Name :";
gets(name);
cout<<"\nAccount number :";
cin>>acc_num;
cout<<"\nAccount type\n1. Saving Account\
n2.Current Account\n";
cin>>acc_type;
cout<<"\nAccount balance:";
cin>>balance;
}
//to display balance
void display()
{
cout<<"\nYour Account
Balance :"<<balance;
}

//to withdraw money from account


void withdraw()
{
cout<<"\nEnter the amount you want to
withdraw :";
cin>>amount;
if(amount>balance)
cout<<"\nInsuficient balance";
else
balance=balance-amount;
display();
}
};
//class current account
class cur_acct:public account
{
public:
void panelty()
{
if(balance<200 && acc_type==2)
{
balance=balance-20;
display();
}
}
};

//class saving account


class sav_acct:public account
{
public:
void interest()
{
int t;
cout<<"\nEnter time interval in year:";
cin>>t;
balance=balance*(1+2*t);
display();
}
};

//main() to test account


int main()
{
account ac;
ac.getData();
ac.display();
ac.withdraw();
}

OUTPUT:
3. Declare a class to represent
fixed-deposit account of 10
customers with the following data
members:
Name of the depositor, Account
Number, Time Period (1 or 3 or 5
years), Amount.
The class also contains following
member functions:
(a) To initialize data members.
(b) For withdrawal of money (after
alf of the time period has passed).
(c) To display the data members.
CODE:
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
class account
{
char name[20];
int time;
double amount=0;
public:
long long int acc_no;
void get_data( char nm[],long long
int n,double amount)
{
cout<<"\nEnter your name:";
cin>>strcpy(name, nm);
acc_no = n;
cout<<"Enter your account
number:";
cin>>n;
time = 1;
}
void withdraw()
{
int a;
double d;
cout<<"Enter Amount:";
cin>>amount;
cout<<"\nEnter the time
period,that has passed after
creating new fixed-deposit
account:";
cin>>a;
cout<<"\nThe amount to be
drawn:";
cin>>d;

if(a>=6)
{
if(d<amount)
{
amount = amount - d;
cout<<"Your current
balance:"<<amount;
}
else
cout<<"\nBalance is less in
your account.Abort!";
}
else
cout<<"\nMoney cannot be
withdrawn. For details contact
helpline.";
}

void display_data()
{
cout<<"Account number:
"<<acc_no<<endl<<"Name:
"<<name<<endl<<"Amount:
"<<amount<<endl;
}

};

OUTPUT:
4. Create two classes DM and DB
which store the value of distances.
DM stores distances in meters and
centimeters and DB in feet and
inches. Write a program that can
read values for the class objects
and add one object of DM with
another object of DB. Use a friend
function to carry out the addition
operation. The object that stores
the results may be a DM object or
DB object, depending on the units
in which the results are required.
The display should be in the
format of feet and inches or
meters and centimeters
depending on the object on
display.
CODE:
#include<iostream>
#include<conio.h>
using namespace std;
class DB;
class DM
{

float meter,centimeter;
public:
void getdata()
{
cout<<"Enter the distance in
(meter-centimeter):";
cin>>meter>>centimeter;
}
void displaydata()
{
cout<<"\nThe distance is:";
cout<<meter<<"meters and
"<<centimeter<<" centimeter";
}
friend void add(DM &,DB &);
};
class DB
{
float inch,feet;
public:
void getdata()
{
cout<<"\nEnter the distance in
(feet-inch):";
cin>>feet>>inch;
}
void displaydata()
{
cout<<"\nThe distance is:";
cout<<feet<<"feet and
"<<inch<<" inch";
}
friend void add(DM &,DB &);
};
void add(DM &a,DB &b)
{
int ch;
cout<<"\nPress 1 for meter-centi:";
cout<<"\nPress 2 for feet-inch:";
cout<<"\nEnter your choice:";
cin>>ch;
if(ch==1)
{
DM d;
int
c=(a.meter*100+a.centimeter+b.feet*30.48
+b.inch*2.54);
if(c>=100)
{
d.meter=c/100;
d.centimeter=c%100;
}
else
{
d.meter=0;
d.centimeter=c;
}
d.displaydata();
}
else
{
DB d;
int
i=(a.meter*39.37+a.centimeter*.3937008+
b.feet*12+b.inch);
if(i>=12)
{
d.feet=i/12;
d.inch=i%12;
}
else
{
d.feet=0;
d.inch=i;
}
d.displaydata();
}
}
int main()
{
DM a;
DB b;
a.getdata();
b.getdata();
add(a,b);
getch();return 0; }

OUTPUT:

Inheritance programs:
1. Write a Program to describe about
all types of inheritance.
CODE:
#include<iostream>
using namespace std;
class A
{
public:
void display()
{
cout<<"\nI am base class A";
}
};
class a
{
public:
void display()
{
cout<<"\nI am another base
class a";
}
};
class B:public A
{
public:
void b()
{
cout<<"\nI am a derived class
B from base class A\nI exhibit Single
Inheritance\n";
}
};
class C:virtual public a,virtual public A
{
public:
void view()
{
A::display();
a::display();
cout<<"\nI am a derived class
C from base classes A and a\nI exhibit
Multiple Inheritance";
}
};
class D:public A
{
public:
void d()
{
cout<<"\n\nI am the second
derived class D from base class A\nI
exhibit Hierarchical inheritance";
}
};
class E:public B
{
public:
void e()
{
cout<<"\n\nI am a derived
class E which was derived from another
derived class B\nI exhibit Multi level
inheritance";
}
};
class F:virtual public A,virtual public
a,public C
{
public:
void f()
{
cout<<"\n";
C::view();
cout<<"\nI am a derived class
F. I was inherited from two base
classes A,a and one multiple
inheritance class C\nIt can be seen that
I was inherited as a combination of
Hierarchical and multiple inheritance. \
nSo, I exhibit Hybrid Inheritance.";
}
};
int main()
{
B b1;
b1.display();
b1.b();
C c;
c.view();
D d1;
d1.d();
E e1;
e1.e();
F f1;
f1.f();

}
OUTPUT:
2. Create a base class called shape.
Use this class to store two double
type values that could be used to
compute the area of figures. Derive
two specific classes called triangle
and rectangle from the base shape.
Add to the base class, a member
function get_data () to initialize
base class data members and
another member function
display_area () to compute and
display the area of figures. Make
display_area () as a virtual function
and redefine this function in the
derived classes to suit their
requirements. Using these three
classes, design a program that will
accept dimensions of a triangle or a
rectangle interactively, and display
the area.

CODE:
#include<iostream>
using namespace std;
class Shape
{
public:
double base,height;
Shape()
{
base=0;
height=0;
}

void get_data_t()
{
cout<<"\nEnter the values of
base and height for triangle:";
cin>>base>>height;
}

void get_data_r()
{
cout<<"\nEnter the values of
base and height for rectangle:";
cin>>base>>height;
}

virtual void display_area()


{
}
};
class Triangle:public Shape
{
public:
void display_area()
{
cout<<"\nArea of
triangle:"<<(base*height)/2;
}
};
class Rectangle:public Shape
{
public:
void display_area()
{
cout<<"\nArea of
rectangle:"<<base*height;
}
};
int main()
{
Shape *s;
Triangle t;
t.get_data_t();
s=&t;
s->display_area();
Rectangle r;
r.get_data_r();
s=&r;
s->display_area();

}
OUTPUT:
3. An educational institution wishes to
maintain a database of its
employees. The database is divided
into a number of classes whose
hierarchical relationships are shown
in following figure. The figure also
shows the minimum information
required for each class. Specify all
classes and define functions to
create the database and retrieve
individual information as and when
required.
CODE:
#include<iostream>
using namespace std;
class staff
{
protected:
int c;
string n;
public:
void sinput()
{
cout<<"\nEnter code : ";
cin>>c;
cout<<"\nEnter name : ";
cin>>n;
}
void idisplay()
{
cout<<"\nCode : "<<c;
cout<<"\nName : "<<n;
}
};
class education
{
protected:
string eq;
string pq;
public:
void einput()
{
cout<<"\nEnter Educational
Qualification: ";
cin>>eq;
cout<<"\nEnter Professional
Qualification: ";
cin>>pq;
}
void edisplay()
{
cout<<"\nEducational Qualification:
"<<eq;
cout<<"\nProfessional Qualification:
"<<pq;
}
};
class teacher : public staff, public
education
{
protected:
string s;
string p;
public:
void tinput()
{
sinput();
cout<<"\nEnter subject: ";
cin>>s;
cout<<"\nEnter publication: ";
cin>>p;
einput();
}
void tdisplay()
{
idisplay();
cout<<"\nSubject: "<<s;
cout<<"\nPublication: "<<p;
edisplay();
}
};
class officer : public staff, public
education
{
protected:
string g;
public:
void oinput()
{
sinput();
cout<<"\nEnter grade: ";
cin>>g;
einput();
}
void odisplay()
{
idisplay();
cout<<"\nGrade: "<<g;
edisplay();
}
};
class typist : public staff
{
protected:
double s;
public:
void tpinput()
{
sinput();
cout<<"\nEnter speed: ";
cin>>s;
}
void tydisplay()
{
idisplay();
cout<<"\nSpeed "<<s;
}
};
class regular : public typist
{
protected:
double sal;
public:
void input()
{
tpinput();
cout<<"\nEnter monthly salary : ";
cin>>sal;
}
void display()
{
tydisplay();
cout<<"\nSalary: "<<sal;
}
};
class causal : public typist
{
protected:
double sal;
public:
void input()
{
tpinput();
cout<<"\nEnter daily salary: ";
cin>>sal;
}
void display()
{
tydisplay();
cout<<"\nSalary : "<<sal;
}
};
int main()
{
int c,d;
cout<<"\nEnter 1 for teacher";
cout<<"\nEnter 2 for typist";
cout<<"\nEnter 3 for officer";
cout<<"\nEnter your choice: ";
cin>>c;
if(c==1)
{
teacher t;
t.tinput();
t.tdisplay();
}
else if(c==3)
{
officer o;
o.oinput();
o.odisplay();
}
else if(c==2)
{
cout<<"\nEnter 1 for regular";
cout<<"\nEnter 2 for causal";
cout<<"\nEnter your choice: ";
cin>>d;
if(d==1)
{
regular r;
r.input();
r.display();
}
else if(d==2)
{
causal c;
c.input();
c.display();
}
}
return 0;
}

OUTPUT:
TEACHER INFORMATION

REGULAR TYPIST INFORMATION:


OFFICER INFORMATION:
POLYMORPHISM PROGRAMS:
Write a program to describe about
virtual function.
CODE:

#include <iostream>
using namespace std;

class base {
public:
virtual void print()
{
cout << "print base class" << endl;
}

void show()
{
cout << "show base class" << endl;
}
};

class derived : public base {


public:
void print()
{
cout << "print derived class" <<
endl;
}

void show()
{
cout << "show derived class" <<
endl;
}
};

int main()
{
base* bptr;
derived d;
bptr = &d;

// virtual function, binded at runtime


bptr->print();

// Non-virtual function, binded at


compile time
bptr->show();
}

OUTPUT:
Explanation: Runtime polymorphism is
achieved only through a pointer (or
reference) of base class type. Also, a base
class pointer can point to the objects of base
class as well as to the objects of derived
class. In above code, base class pointer
‘bptr’ contains the address of object ‘d’ of
derived class.
Late binding(Runtime) is done in accordance
with the content of pointer (i.e. location
pointed to by pointer) and Early
binding(Compile time) is done according to
the type of pointer, since print() function is
declared with virtual keyword so it will be
bound at run-time (output is print derived
class as pointer is pointing to object of
derived class ) and show() is non-virtual so it
will be bound during compile time(output
is show base class as pointer is of base
type ).

EXCEPTION HANDLING
PROGRAMS
1. Write a Program to describe about
exception handling mechanism.

CODE:
#include <iostream>
using namespace std;

int main()
{
int x = -1;

// Some code
cout << "Before try \n";
try {
cout << "Inside try \n";
if (x < 0)
{
throw x;
cout << "After throw (Never
executed) \n";
}
}
catch (int x ) {
cout << "Exception Caught \n";
}

cout << "After catch (Will be


executed) \n";
return 0;
}

OUTPUT:
2. Write a Program to describe multi
catch statement.

CODE:
#include<iostream>
using namespace std;
void test(int x)
{
try
{
if(x==1)throw x;
else if(x==0)throw 'x';
else if(x==-1)throw 1.0;
cout<<"End of try-block \n";
}
catch(char c)
{
cout<<"Caught a character \n";
}
catch(int m)
{
cout<<"Caught an integer \n";
}
catch(double d)
{
cout<<"Caught a double \n";
}
cout<<"End of try-catch system \n\n";
}
int main()
{
cout<<"testing Multiple Catches \n";
cout<<"x ==1 \n";
test(1);
cout<<"x ==0 \n";
test(0);
cout<<"x ==-1 \n";
test(-1);
cout<<"x ==2 \n";
test(2);
return 0;
}

OUTPUT:

THANK
YOU

You might also like