0% found this document useful (0 votes)
6 views45 pages

Cs 305 (Oopm) Lab Experiments

The document outlines a series of C++ programming experiments focused on object-oriented programming concepts. Each experiment includes an aim, procedure, and sample code, covering topics such as class creation, struct declaration, inheritance, and console I/O operations. The document serves as a practical guide for students to implement and understand various OOP principles in C++.

Uploaded by

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

Cs 305 (Oopm) Lab Experiments

The document outlines a series of C++ programming experiments focused on object-oriented programming concepts. Each experiment includes an aim, procedure, and sample code, covering topics such as class creation, struct declaration, inheritance, and console I/O operations. The document serves as a practical guide for students to implement and understand various OOP principles in C++.

Uploaded by

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

OOPM

CS-305
(OOPM)
LIST OF
EXPERIMENTS
LISTOFEXPERIMENTS
1. Write a C++ Program to display Names, Roll No., and grades of 3 students who have
appearedintheexamination.Declaretheclassofname,RollNo.andgrade.Createan array of
class objects. Read and display the contents of the array.
2. WriteaC++programtodeclareStruct. Initializeanddisplaycontentsofmembervariables.
3. WriteaC++programtodeclareaclass.Declarepointertoclass.Initializeanddisplaythe contents
of the class member.
4. GiventhatanEMPLOYEEclasscontainsfollowingmembers:datamembers:Employee
number, Employee name, Basic, DA, IT, Net Salary and print data members.
5. WriteaC++programtoreadthedataofNemployeeand computeNetsalaryofeach
employee (DA=52% of Basic and Income Tax (IT) =30% of the gross salary).
6. WriteaC++toillustrate theconceptsofconsole I/O operations.
7. WriteaC++programtousescoperesolutionoperator.Displaythevariousvaluesofthe same
variables declared at different scope levels.
8. WriteaC++program toallocatememoryusingnewoperator.
9. WriteaC++programtocreatemultilevelinheritance.(Hint:ClassesA1,A2,A3)
10. WriteaC++program tocreateanarrayofpointers.Invokefunctionsusing arrayobjects.
11. WriteaC++programtousepointerforbothbaseandderivedclassesandcallthemember function.
Use Virtual keyword
EXPERIMENT-1

AIM: Write a C++ program to display names, roll no and grades of 3 students who have
appeared in examination. Declare the class of name roll no and grade. Create anarray of
class objects .read and display the contents of array.

PROCEDURE:
Step1-Includetherequiredheaderfiles(iostream.handconio.h).
Step2-Createaclass(StudentInfo)withthefollowingclassmembersaspublicmembers. student_name,
roll_number and grade as data members.
read_info()anddisplay_info()asmemberfunctions.
Step3-Implementtheread_info()anddisplay_info()memberfunctions. Step 4 -
Create a main() method.
Step5-Createanobjectoftheabove class insidethemain()method.
Step6-Makefunctioncallstoread_info()anddisplay_info()usingclass object.

PROGRAM:

#include <iostream>
usingnamespacestd;
class Student_Info{
int roll_number;
charstudent_name[50],grade[2];
public:
voidread_data(intcount){
cout<<"\n\n---------Enterstudent"<<count+1<<"information-----------------\n";
cout<<"NameoftheStudent(Max.50charactersonly):";
cin>>student_name;
cout<<"RollNumber:";
cin>>roll_number;
cout<<"Grade(O,A+,A,B+,B,C,D,F): ";
cin>>grade;
cout<<"\nStudentinformationwithrollnumber"<<roll_number<<"has saved!";
}
voiddisplay_data(intcount){
cout<<"\n\n********Student"<<count+1<<"Information********"; cout<<"\nName of
the Student: "<<student_name;
cout<<"\nRoll Number: "<<roll_number;
cout<<"\nGrade Secured: "<<grade; cout<<"\
n \n";
}
};
int main()
{ Student_Infostud[3
]; int i;
for(i=0; i<3; i++)
stud[i].read_data(i);
cout<<"\n\n+++++++++++++++++++++++++++++++++++++++++++++++\n";
cout<<"The information of 3 students has been saved."; cout<<"\n+++++++++++++++++++++++
++++++++++++++++++++++++\n";
for(i=0; i< i++)
stud[i].display_data(i);
return0}
OUTPUT:
EXPERIMENT–2

AIM:Writeac++programtodeclarestruct,intializeanddisplaycontentsofmember variables

PROCEDURE:

 Step1-Includetherequiredheaderfiles(iostream.handconio.h).
 Step2-Createastructure(college_info)withthefollowingmembervariables.
college_name, college_code, dept, and intake as data members.
 Step3-Create amain() method.
 Step4 -Createavariableoftheabovestructureinsidethemain()method.
 Step5-Then,readdataintomembervariablesofthatstructureusingstructurevariable with dot
operator.
 Step 6 -Finally, displaydata of member variables of that structure using structure variable
with dot operator.

PROGRAM:

#include <iostream>
usingnamespacestd;
struct college_info{
charcollege_name[15];
char college_code[2];
char dept[50];
int intake;
};
int main()
{
structcollege_info college;
cout<<"\n+++++EntertheCollegeInformation+++++\n\n";
cout<<"Name of the college: ";
cin>>college.college_name;
cout<<"College Code: ";
cin>>college.college_code;
cout<<"Department: ";
cin>>college.dept;
cout<<"DepartmentIn-take:";
cin>>college.intake;
cout<<"\n\n*********CollegeInformation*********\n\n";
cout<<"Name of the college : "<<college.college_name; cout<<"\
nCollegeUniversityCode:"<<college.college_code; cout<<"\nName of
the Department: "<<college.dept;
cout<<"\nThedepartmentof"<<college.dept<<"hasin-take:"<<college.intake;
cout<<"\n\n \n\n";
return0;
}

OUTPUT:
EXPERIMENT-3

AIM:WriteaC++programtodeclareaclass, declarepointertoclass,initializeanddisplay contents of


class member.

PROCEDURE:

 Step1-Includetherequiredheaderfiles(iostream.h,conio.h,andwindows.hforcolors).
 Step 2 -Create a class (Rectangle) with the following members as public members. length
and breadth as data members. initialize(), getArea() and display() as member functions.
 Step3-Create amain() method.
 Step 4 -Createavariable(rect)and apointervariable(class_ptr)oftheaboveclassinside the
main() method.
 Step5-Assigntheaddressofclassobject(rect)toclasspointerobject(class_ptr).
 Step6-Then,callthememberfunctionsinitialize()anddisplay()usingclasspointerobject
(class_ptr) to illustrate member functions access using class pointer.
 Step 7 -Assign values to data members of the class using class pointer object to illustrate
data members access using class pointer.
 Step8-Finally,callthememberfunctionsinitialize()anddisplay()usingclasspointer object
(class_ptr).

PROGRAM:

#include<windows.h>#i
nclude <iostream>
using namespace std;
class RectangleTest{
public:
intlength,breadth;
public:
voidinitialize(intlen,intbre)
{ length = len;
breadth=bre;
}
intgetArea(){
return2*length*breadth;
}
voiddisplay(){
intarea= getArea();
cout<<"\n***RectangleInformation***\n";
cout<<"Length = "<<length; cout<<"\
nBreadth = "<<breadth; cout<<"\nArea =
"<<area;
cout<<"\n \n";
}
};
int main()
{
RectangleTestrect,*class_ptr;
HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE);
class_ptr = &rect;
//Accessing member functions using class pointer
SetConsoleTextAttribute(color, 10 ); //SettingcolorGreen
cout<<"\nUsing member functions access";
SetConsoleTextAttribute(color, 7 ); //Setting color White
class_ptr->initialize(10,5);
class_ptr->display();
//Accessing data members using class pointer
SetConsoleTextAttribute(color, 10 ); //SettingcolorGreen
cout<<"\nUsing data members access";
SetConsoleTextAttribute(color, 7 ); //Setting color White
class_ptr->length = 2;
class_ptr->breadth=3;
class_ptr->initialize(class_ptr->length,class_ptr->breadth);
class_ptr->display();
return 0;
}

OUTPUT:

.
EXPERIMENT-4

AIM:Giventhatanemployeeclasscontainsfollowingmembers,datamembers,employee number,
employee name ,basic, DA, IT, net salary and print data members

PROCEDURE:

 Step1-Includetherequiredheaderfiles(iostream.h,conio.h,andwindows.hforcolors).
 Step 2 - Create a class (employee) with the following members as public members.
emp_number, emp_name, emp_basic, emp_da, emp_it, and emp_net_sal as data members.
get_emp_details(), find_net_salary() and show_emp_details() as member functions.
 Step 3- Implement all the member functions with their respective code (Here, we have
used scope resolution operator ::).
 Step3-Create amain() method.
 Step 4-Createanobject(emp)oftheaboveclassinsidethemain()method.
 Step5-Callthememberfunctionsget_emp_details()andshow_emp_details().
 Step 6-returnn0to exitformtheprogram execution.

PROGRAM:

#include<windows.h>#i
nclude <iostream>
using namespace std;
class employee
{
int emp_number;
charemp_name[20];
floatemp_basic;
float emp_da;
floatemp_it;
floatemp_net_sal;
public:
voidget_emp_details();
floatfind_net_salary(floatbasic,floatda,floatit); void
show_emp_details();
};
voidemployee:: get_emp_details()
{
cout<<"\nEnteremployeenumber:";
cin>>emp_number;
cout<<"\nEnteremployeename:";
cin>>emp_name;
cout<<"\nEnteremployeebasic:";
cin>>emp_basic;
cout<<"\nEnteremployeeDA:";
cin>>emp_da;
cout<<"\nEnteremployeeIT:"; cin>>emp_it;
}
floatemployee::find_net_salary(floatbasic,floatda,floatit)
{
return(basic+da)-it;
}
voidemployee:: show_emp_details()
{
cout<<"\n\n**** Details of Employee ****";
cout<<"\nEmployee Name : "<<emp_name;
cout<<"\nEmployee number :"<<emp_number;
cout<<"\nBasic salary : "<<emp_basic;
cout<<"\nEmployee DA : "<<emp_da;
cout<<"\nIncome Tax :"<<emp_it;
cout<<"\nNet Salary :"<<find_net_salary(emp_basic,emp_da,emp_it);
cout<<"\n \n\n";
}
int main()
{
employee emp;
emp.get_emp_details();
emp.show_emp_details();
return 0;
}

OUTPUT:
EXPERIMENT-5

AIM:WriteaC++programtoreadthedataofNemployeeandcomputenetsalaryofeach employee
(DA=52%of basic and income tax(IT)=30% of gross salary.

PROCEDURE:

 Step1-Includetherequiredheaderfiles(iostream.h,conio.h,andwindows.hforcolors).
 Step 2 - Create a class Employee with the following members. emp_number, emp_name,
basic, da, it, gross_salary, and net_salary as data members read_emp_details(),
find_net_salary(), and display_emp_details() as member functions.
 Step3-Implementallthememberfunctionswiththeirrespectivecode.
 Step4-Create amain() method.
 Step 5-Createanarrayofclassobjectwithaspecificsizeandnumber_of_empas integer.
 Step6-Readnumber_of_emp.
 Step7-Calltheread_emp_details()methodthroughthearrayofclassobjectfrom0to
number_of_emp.
 Step8-Callthefind_net_salary()methodthroughthearrayofclassobjectfrom0to
number_of_emp.
 Step 9 -Call the display_emp_details() method through the array of class object from 0 to
number_of_emp.
 Step 10-returnn0toexitformtheprogram execution.

PROGRAM:

#include<iostream.h>
#include<conio.h>clas
s Employee
{
charemp_name[30];
int emp_number;
floatbasic,da,it,gross_salary,net_salary;
public:
voidread_emp_details(intcount){
cout<<"\n\n***EnterEmployee"<<count<<"Details***";
cout<<"\nEmployee Number: ";
cin>>emp_number;
cout<<"EmployeeName:";
cin>>emp_name;
cout<<"Basic Salary: ";
cin>>basic;
cout<<"\n----Employee"<<count<<"Datailsaresaved--------\n\n";
}
floatfind_net_salary(){
da = basic * 0.52;
gross_salary=basic+da; it
= gross_salary * 0.30;
net_salary=(basic+da)-it; return
net_salary;
}
voiddisplay_emp_details(intcount){
cout<<"\n\n***Employee"<<count<<"Details***\n";
cout<<"\nEmployee Number : "<<emp_number;
cout<<"\nEmployee Name : "<<emp_name;
cout<<"\nNet Salary: "<<net_salary;
cout<<"\n \n";
}
};
int main(){
Employeeemp[100];
intnumber_of_emp,count;
clrscr();
cout<<"\nPleaseenterthenumberofEmployees(Max.100):";
cin>>number_of_emp;
for(count=0;count<number_of_emp;count++){
emp[count].read_emp_details(count+1);
}
for(count=0;count<number_of_emp;count++){
emp[count].find_net_salary();
}
for(count=0;count<number_of_emp;count++){
emp[count].display_emp_details(count+1);
}
cout<<"\nPressanykeytoclose!!!";
getch();
return 0;
}
OUTPUT:
EXPERIMENT-6

AIM:WriteaC++toillustratetheconceptsofconsoleI/O operations.

Therearemainlytwo types ofconsolIOoperations.


1. UnformattedconsolIO-
2. FormattedconsolIO

6.1.Unformatted consoleIOoperations

We use the following built-in functions to perform operations of type unformatted consol IO
operations.

6.1A)get()andput()
The get( ) is a method of cin object used to input a single character from the standard input device
(keyboard). Its main property is that it allows wide spaces and newline character. Theget() function
does not have any return value (void get( )).
The put() is a method of cout object and it is used to print the specified character on standard
output device (monitor).
PROGRAM:
#include <iostream>
usingnamespacestd;
int main()
{
charch;
cout<<"Pressanykey:"; ch
= cin.get();
cout<<"Youhavepressed:";
cout.put(ch);
return0;
}

OUTPUT:
6.1B)getline(char*buffer,intsize)andwrite(char*buffer,intn)
Thegetline(char*buffer,intsize)isamethodofcinobjectanditisusedtoinputastringwith multiple spaces.
Thewrite (char * buffer, int n)is a method ofcoutobject and it is used to read n character from
buffer variable.
PROGRAM:

#include <iostream>
usingnamespacestd;
int main()
{
charch[20];
cout<<"Whatisyourfavouritewebsite:";
cin.getline(ch, 20);
cout<<endl<<"visit:www.";
cout.write(ch, 17);
cout<<".com"<<endl;
return 0;
}

OUTPUT:

C)cin andcoutobjects
Thecinis the object used to take input value of any variable of any type.It must be used with an
overloaded operator “>>”.
Thecoutis an object used to print string and variable values. It must be used with an
overloadedoperator “<<”.
Example
#include <iostream>
usingnamespacestd;
int main()
{
int variable_int;
floatvariable_float;
charvariable_char;

cout<<"Enteranyintegernumber:";
cin>>variable_int;
cout<<"Enteranyfloatingpointnumber:";
cin>>variable_float;
cout<<"Enteranycharacter:";
cin>>variable_char;
cout<<endl<<"Integer="<<variable_int<<endl;
cout<<endl<<"FloatingPoint="<<variable_float<<endl;
cout <<endl<<"Character = "<<variable_char<<endl;
return 0;
}

OUTPUT:
6.2)Formatted consoleIOoperations

TheC++programminglanguageprovidesthefollowingbuilt-infunctionstodisplaytheoutputin formatted
form. These built-in functions are available in the header file iomanip.h.

A)setw(int)andsetfill(char)
Thesetw(int)is amethodused to set width oftheoutput.
Thesetfill(char)isamethodusedtofillspecifiedcharacteratunusedspace.

PROGRAM:
#include <iostream>
#include <iomanip>
usingnamespacestd;
int main()
{
int x=10;
cout<<setw(10);
cout<<x<<endl;
cout<<setw(10)<<setfill('*')<<x<<endl;
return 0;
}

OUTPUT:
EXPERIMENT-7

AIM:WriteaC++programtousescoperesolutionoperator.Displaythevariousvaluesof the same


variables declared at different scope levels.

7A)AccessingGlobal variable
Whenbothglobal andlocal variableshavethe samenamethenglobalvariableinhiddeninsidethe local
scope. Here, we use the scope resolution operator to refer to the global variable.
PROGRAM:
#include<iostream>
usingnamespacestd;
intmy_variable=10;//Globalvariablemy_variable int
main()
{
intmy_variable=100;//Localvariablemy_variable
cout<<"Valueofglobalmy_variableis"<<::my_variable<<endl; cout
<<"Value of local my_variable is "<< my_variable << endl; return 0;
}

OUTPUT:

7B)Definingafunctionoutof class
Whenaclasshasafunctionprototypeinsidebutthedefinitionisoutsideoftheclass.Here,to define the
function outside the class we use the scope resolution operator.
PROGRAM:
#include <iostream>
usingnamespacestd;
class My_Class
{
public:
intmy_variable=10;
voiddisplay();//Prototypeofdisplayfunction
};
voidMy_Class::display(){//DefiningfunctionusingScopeResolutionOperator cout
<< endl <<"We are in display now!"<< endl;
cout<<"my_variable valueis"<<my_variable<<endl<<endl;
}
int main(){
My_Classobj;
cout<<"Weareinmainnow<<endl; obj.display();
cout<<"Weareagaininmail!!"<<endl;
return 0;
}
OUTPUT:

7C)Accessingstaticmembersofaclass
Thescope resolution operator can be used to access static members of a class when thereis alocal
variable with the same name.
PROGRAM:
#include <iostream>
usingnamespacestd;
class StaticTest
{
staticintx;
public:
staticint y;
//Localparameter'x'hidesclassmember
//'x',butwecanaccessitusing:: void
my_function(int x)
{
//Wecan accessclass'sstaticvariable
//evenifthereis alocalvariable
cout<<"Valueofstaticxis"<<StaticTest::x; cout
<<"\nValue of local x is "<< x;
}
};
//InC++,staticmembersmustbeexplicitlydefinedlikethis int
StaticTest::x = 1;
intStaticTest::y=2; int
main()
{
StaticTestobj;
int x = 3 ;
obj.my_function(x);
cout<<"\n\nStaticTest::y="<<StaticTest::y<<endl; return 0;
}

OUTPUT:
7D)ScopeResolutionwithMultipleInheritance
#include<iostream>

usingnamespacestd;

classBaseClass_1{ publ
ic:
intvar_a=10;
voiddisplay(){
cout<<"IaminBaseClassdisplay()"<<endl;
cout<<"var_a = "<<var_a;
}
};

classBaseClass_2{ publ
ic:
intvar_a=20;
voiddisplay(){
cout<<"IaminBaseClass_2display()"<<endl;
cout<<"var_a = "<<var_a;
}
};
classDerivedClass:publicBaseClass_1,publicBaseClass_2{ pu
blic:
intvar_a=30;
voiddisplay(){
cout<<"I am in DerivedClass display()"<<endl<<endl;
cout<<"BaseClass_1var_a="<<BaseClass_1::var_a<<endl;
cout<<"BaseClass_2var_a="<<BaseClass_2::var_a<<endl;
cout<<"DerivedClass var_a = "<<var_a<<endl;
}
};
int main()
{
DerivedClassobj;
obj.display();
return 0;
}

OUTPUT:
EXPERIMENT-8
AIM:WriteaC++programtoallocatememory usingnewoperator

PROGRAM:
#include <iostream>
usingnamespacestd;
int main()
{int *ptr;
ptr=newint;//Dynamicmemoryallocation
cout<<"Numberofbytesallocatedtoptris "<<sizeof(ptr)<<endl;
*ptr= 100;
cout<<"Valueatptris"<<*ptr<<endl; return
0;
}

OUTPUT:
//newwithuser-defineddatatypevariable(Objects)
#include <iostream>
usingnamespacestd;
class Rectangle{
intlength,width;
public:
Rectangle(){
length=2;
width=5;
}
Rectangle(intl,intw)
{ length = l;
width=w;
}
void area(){
cout<<"AreaofRectangleis"<<length *width <<endl;
}
};
int main()
{
Rectangle*obj_1,*obj_2;
obj_1 = new Rectangle(); // Dynamic memory allocation
obj_2=newRectangle(3,10);//Dynamicmemoryallocation
obj_1->area();
obj_2->area();
return 0;
}
OUTPUT:
EXPERIMENT-9
AIM:WriteaC++programtocreatemultilevelinheritance.(Hint:ClassesA1,A2,A3)

.PROGRAM:
#include<iostream>
usingnamespacestd;

classParentClass{
int a;
public:
ParentClass(){
a = 10;
}
void show_a(){
cout<<endl<<"InsidetheParentClassshow_amethod!"<<endl;
cout<<"value of a is "<< a << endl;
}};
classChildClass_1:publicParentClass{ int
b;
public:
ChildClass_1(){
b = 100;
}
void show_b(){
cout<<endl<<"InsidetheChildClass_1show_bmethod!"<<endl;
cout<<"value of b is "<< b << endl;
}
};
classChildClass_2:publicChildClass_1{ int
c;
public:
ChildClass_2(){
c = 1000;
}
void show_c(){
cout<<endl<<"InsidetheChildClass_2show_cmethod!"<<endl;
cout<<"value of c is "<< c << endl;
}};
int main()
{
ChildClass_2obj;
obj.show_a();
obj.show_b();
obj.show_c();
return 0;
}

OUTPUT:
EXPERIMENT-10
AIM:WriteaC++programtocreateanarrayofpointers.Invokefunctionsusingarray objects.

PROCEDURE:

 Step1-Includetherequiredheaderfiles(iostream.handconio.h).
 Step 2 - Create a class (Student) with the following class members as public members.
student name, marks as data members. getStudentInfo() and displayStudentInfo() as
member functions.
 Step3-ImplementthegetStudentInfo()anddisplayStudentInfo()memberfunctions.
 Step4-Create amain() method.
 Step 5-Createan arrayofstudobjectwigthmaxsizeandapointerobjectofStudent class.
 Step6-Assignbaseaddressof objectarray topointerobjectandmakefunctioncallsto
getStudentInfo() and displayStudentInfo() using class pointer object.

PROGRAM:

#include<iostream>
usingnamespacestd;
#define max 100
class Student
{
stringstud_name;
int marks;
public:
voidgetStudentInfo(inti)
{
cout<<endl<<"Enterthestudent"<<i<<"details"<<endl;
cout<<"Name of the Student: ";
cin>>stud_name;
cout<<"Markssecured:"; cin>>
marks;
}
voiddisplayStudentInfo()
{
cout<<"NameoftheStudent:"<<stud_name<<endl; cout
<<"Marks secured : "<< marks << endl;
}
};
int main()
{
Studentstud[max],*ptr;
int class_size;
ptr=stud;
cout<<"Enterthenumberofstudentsintheclass(<"<<max<<"):"; cin>>
class_size;
for(inti=1;i<=class_size;i++)
{
(ptr+i)->getStudentInfo(i);
}
cout<<endl<<"*****Enteredstudentdata*****"<<endl;
for( int i=1; i<=class_size; i++ )
{
cout<<"Student"<<i<<endl;
(ptr+i)->displayStudentInfo();
}
return 0;
}
OUTPUT:
EXPERIMENT-11
AIM:WriteaC++programtousepointerforbothbaseandderivedclassesandcallthe member
function. Use Virtual keyword.

PROGRAM:
#include<iostream>
usingnamespacestd;

classWeapon
{
public:
virtualvoid features()
{
cout<<"Loadingweapon features.\n";
}
};
classBomb :publicWeapon
{
public:
void features()
{
this->Weapon::features();
cout<<"Loadingbomb features.\n";
}
};
classGun: publicWeapon
{
public:
void features()
{
this->Weapon::features();
cout<<"Loadinggunfeatures.\n";
}
};
classLoader
{
public:
voidloadFeatures(Weapon*weapon)
{
weapon->features();
}
};
int main()
{
Loader*l=newLoader;
Weapon *w;
Bombb;
Gun g;
w=&b;
l-
>loadFeatures(w);w
= &g;
l-
>loadFeatures(w);ret
urn 0;
}
OUTPUT:
ADDITIONALPROGRAMS

1. Writea c++programforInlinefunction.

#include <iostream>
usingnamespacestd;
class operation
{
inta,b,add,sub,mul;
float div;
public:
void get();
voidsum();
voiddifference();
void product();
void division();
};
inlinevoidoperation:: get()
{
cout<<"Enterfirstvalue:"; cin
>> a;
cout<<"Entersecondvalue:"; cin
>> b;
}

inlinevoidoperation :: sum()
{
add= a+b;
cout<<"Addition oftwonumbers: "<<a+b<<"\n";
}

inlinevoidoperation:: difference()
{
sub=a-b;
cout<<"Differenceoftwonumbers: "<<a-b<<"\n";
}

inlinevoidoperation:: product()
{
mul= a*b;
cout<<"Productoftwonumbers:"<<a*b<<"\n";
}

inlinevoidoperation::division()
{
div=a/b;
cout<<"Divisionoftwo numbers:"<<a/b<<"\n";
}

int main()
{
cout<<"Programusinginlinefunction\n"; operation
s;
s.get();
s.sum();
s.difference();
s.product();
s.division();
return 0;
}

OUTPUT:

Enter first value: 45


Entersecondvalue:15
Addition of two numbers: 60
Differenceoftwonumbers:30
Product of two numbers: 675
Division of two numbers: 3

2. WriteaC++programonSinglelevel inheritance

#include<iostream>

usingnamespacestd;

class base //singlebaseclass

public:

intx;

void getdata()

cout<<"Enterthevalue ofx=";cin >>x;

}
};

classderive:publicbase //singlederivedclass

private:

int y;

public:

void readdata()

cout<<"Enterthevalueof y=";cin>>y;

void product()

cout<<"Product ="<<x* y;

};

int main()

derive a; //objectofderivedclass

a.getdata();

a.readdata();

a.product();

return 0;

}
OUTPUT:

Enterthevalueofx=3

Enterthevalueofy=4

Product = 12

3. Writeac++program formultiplecatch statement

#include<iostream.h>

#include<conio.h>voi

d main()

inta=2;

try

if(a==1)

throw a; //throwingintegerexception

else if(a==2)

throw 'A'; //throwingcharacterexception

else if(a==3)

throw 4.5; //throwingfloatexception

catch(inta)

cout<<"\nIntegerexceptioncaught.";

catch(charch)
{

cout<<"\nCharacterexceptioncaught.";

catch(doubled)

cout<<"\nDoubleexceptioncaught.";

cout<<"\nEndof program.";

OUTPUT:

Characterexceptioncaught.

End of program.

4. WriteaC++programforuserdefined manipulators.

#include<iostream.h>
#include<iomanip.h>

voidmain (void)
{
int value;
floata,b,c;
a=350;
b = 200;
c=a/b;

cout<<"Enternumber"<<endl; cin
>> value;
cout<<"Hexadecimalbase="<<hex<<value<<endl; cout <<"
Octal base ="<< oct << value << endl;
cout<<"hexadecimalbase="<<setbase(16); cout <<
value << endl;
cout<<"Octalbase="<<setbase(8)<<value<<endl; cout
<< setfill ('*');
cout<<setw(5)<<a<<setw(5)<<b<<endl;
cout<<setw(6)<<a<<setw(6)<<b<<endl;
cout<<setw(7)<<a<<setw(7)<<b<<endl;

cout<<fixed <<setprecision(2)<<c<<endl;

OUTPUT:

Enternumber
100
Hexadecimalbase =64
Octal base =144
hexadecimalbase=64
Octal base = 144
**350**200
***350***200
****350****200
1.75

You might also like