Table of Contents
Important Questions for Class 12 Computer Science – Object Oriented Programming
Question 1:
Write any four important characteristics of object oriented programming. Give example of any one of the characteristics using C++. All India 2016
Answer:
Four important characteristics of object oriented programming are as follows:
(i) Data encapsulation
(ii) Inheritance
(iii) Polymorphism
(iv) Data abstraction
Example of Inheritance
Class Rectangle { :::: }; class Area : public Rectangle { :::: };
Question 2:
Explain data hiding with an example. All India 2014C
Answer:
Data hiding is a property, where internal data structure of an object is hidden from the outside world. Data hiding helps to secure the data. It is implemented with private and protected keywords. ,
e.g.
class Item { private: int item_no; float item_cost; public: void getdata(); void putdata(); };
Here, in this example variables item_no and item_cost are hidden, i.e. cannot be directly accessed from outside the class Item.
Question 3:
What is function overloading? Give an example in C++ to illustrate function overloading.
All India 2014,2009
or
What do you understand by function overloading? Give an example illustrating its use in a C++ program. All India 2009
Answer:
When several functions have same name but performing different tasks, then it is known as function overloading. The definitions of these functions are differentiable by the number or types of their arguments.
e.g.
float compute(float radius) { return(3.14*radius*radius); } float compute(float l, float b) { return(l*b) } float compute (int b, float h) { return(0.5*b*h); }
Question 4:
Write the output of the following C++ code. Also, write the name of feature of Object Oriented Programming used in the following program jointly illustrated by the functions [I] to [IV]. Delhi 2011
#include<iostream.h> void Print() //Function[I] { for(int K=1; K<=60; K++) cout<<"-"; cout<<endl; } void Print(int N) //Function[II] { for(int K=1; K<=N; K++) cout<<"*"; cout<<endl; } void Print(int A, int B)//Function[III] } for(int K=1; K<=B; K++) cout<<A*K; cout<<endl; } void Print(char T, int N)//Function[IV] { for(int K=l; K<=N; K++) cout<<T; cout<<endl; } void main!) { int U=9, V=4, W=3; char C= '@'; Print(C, V); Print(U, W); }
Answer:
Feature of C++→ Function overloading The output will be:
@@@@
91827
Question 5:
Write the output of the following C++ code. Also, write the name of feature of Object Oriented Programming used in the following program jointly illustrated by the functions [I] to [IV]: All India 2011
#include<iostream.h> void Lined //Function[I] { for(int L=1;L<=80;L++) cout<<"-"; cout<<endl; } void Line(int N) //Function[II] { for(int L=1;L<=N;L++) cout«"*"; cout<<endl: } void Line(char C,int N) //Function[III] { for(int L=1:L<=N:L++) cout<<C; cout<<endl: } void Line(int M, int N)//Function[IV] { for(int L=1:L<N;L++) cout<<M*L; cout<<endl; } void main() { int A=9, B=4, C=3; char K=’#': Line (K,B); Line (A,C);
Answer:
Feature of C ++ Function overloading Output will be :
####
91827
Question 6:
What do you understand by Data Encapsulation and Data Hiding? Also, give an example in C++ to illustrate
both. All India 2010
Answer:
Data Encapsulation
The wrapping up of data (member variables) and functions into a single unit is known as data encapsulation. Encapsulation is implemented in C ++ with the help of classes.
Data Hiding
When member data and member function are binded into a single unit then the data is not accessible to the outside world and only those functions, which are wrapped in that class can access it. Data hiding is implemented in C+ + with the help of private and protected keywords.
e.g.
Question 7:
What do you understand by polymorphism? Give an example illustrating its use in a C++ program. Delhi 2010
Answer:
Polymorphism means processing of data or messages in more than one form. C ++ implements polymoiphism through overloaded functions and overloaded operators,
e.g.
float compute(float a) { return a*a; } float computer float a,float b) { return(a*b); }
Question 8:
How are abstraction and encapsulation interrelated? Delhi 2009
Answer:
Abstraction refers to the representation of only the essential features of the real-world object in the program. This process does not include the background details and explanations. This concept of abstraction is used in classes whereas, data encapsulation is the most significant characteristic of the class. By this term, we mean the wrapping up of data and functions which operate on the data, into a single unit called the class. This encapsulation prevents free access to the data within an object.
Question 9:
What is event driven programming?
Answer:
In event driven programming, the user indicates the order of program execution not the programmer. Instead of, the program ‘driving’ the user ‘drives’ the program. Programming, in which the code that responds to the event is called event driven programming.
Question 10:
What is the significance of classes in OOPs?
Answer:
The classes are the manufacturing units of the objects of their type, i.e. it is the class that can be used to create an object. Classes are user defined data types and behave like the built-in types of a programming language. Classes are the basic building blocks of object oriented programming.
Question 11:
What are the advantages of object oriented programming over procedural oriented programming?
Answer:
Advantages of Object Oriented Programming (OOP) over Procedural Oriented Programming (POP)
Object Oriented Programming | Procedural Oriented Programming |
In OOP, program is divided into parts called objects. | In POP, program is divided into small parts called functions. |
OOP follows bottom up approach. | POP follows top down approach. |
OOP provides data hiding that provides more security. | POP does not have any proper way for data hiding so it is less secure. |
C++, Java, VB.NET, C, .NET, etc. are the examples of OOP language. | C, VB, FORTRAN, Pascal etc are the examples of POP language. |
Question 12:
Encapsulation is one of the major properties of OOP. How is it implemented in C++? HOTS
or
Define the term data encapsulation in terms of object oriented programming. Give a suitable example using a C++ code to illustrate the same.
Answer:
The wrapping up of data and functions into a single unit is called data encapsulation. That single unit is known as class.
e.g.
class person { char name[30]; int age; public: void getdata(void); void display(void); };
The above program implements data hiding as data can’t be accessed directly from outside.
Question 13:
What is operator overloading? Explain with example.
Answer:
The process of making an operator to exhibit or show different behaviour in different situations is called as operator overloading.
e.g. consider the operation of (+) operator. Operation is sum, if operands are integer type and the operation is concatenation, if operands are strings.
Important Questions for Class 12 Computer Science – Classes and Objects
Question 1:
What is the relationship between a class and an object? Illustrate with a suitable example. Delhi 2013C
Аnswers:
Class
• It is a user-defined data type.
• It is a logical framework that combines data and function into a single unit.
e.g
class GABS { int a,b; public: void set() { a=10; b=20; } void show() { cout<<"Value of a and b is"<<a<<b<<endl; };
Object
• It is an instance of a class.
• It is a physical entity that means it occupies space in a memory. It’s used to access members of the class.
e.g. Suppose “GABS” is the name of the class.
GABS G1, G2;
G1.show( );
Both G1 and G2 are the objects of the class “GABS”
Question 2:
Rewrite the following program after removing the syntactical errors (if any). Underline each correction. Delhi 2012
#include<iostream.h> class Book { long Bld.Qty; public: void Purchase() { cin>>BId<<Qty; } void Sale { cout<<setw(5)<<BId<<"o1d:”<<Qty<<endl; cout<<"New:"<<—Qty<<endl; } }; void main() { Book B; B.Purchase(); Sale(); B.Sale(); }
Аnswers:
#include<iostream.h> #include<iomanip.h> class Book { long Bid, Qty; pub!ic: void Purchase() { cin>>BId>>Qty; } void Sale() { cout<<setw(5)<<BId<<"o1d:"<<Qty<<endl; cout<<"New:"<<--Qty<<endl; } }; void main() { Book B; B.Purchase(); B.Sale(); B.Sale(); }
Question 3:
Rewrite the following program after removing the syntactical errors (if any). Underline each correction. Delhi 2012
#include<iostream.h> class Item { long IId, Qty; public: void Purchase { cin>>IId>>Qty; } void Sale() { cout<<setw(5)<<IId<<”old:"<<Qty<<endl; cout<<"New: "<<--Qty<<endl; } }; void main() { Item I; Purchase(): I.Sale(); I.Sale(); }
Аnswers:
#include<iostream.h> #include<iomanip.h> class Item { long lid,Qty; public: void Purchase() { cin>>IId>>Qty; } void Sale() { cout<<setw(5)<<IId<<"01d:"<<Qty<<endl; cout<<"New:"<<--Qty<<endl; } }; void main() { Item I; I.Purchase(); I.Sale(); I.Sale(); }
Question 4:
Rewrite the following program after removing the syntactical error(s) (if any). Underline each correction. Delhi 2012C
#include<iostream.h> #include<stdio.h> Class OFFER { int OfferId; char Description[80]: void Show { cout<<offerId<<" : ”<<Description<<endl; } public: void Enter { cin>>offerId; gets>>Description; } }; void main() { OFFER Obj; Obj.Enter(); Obj.Show(); }
Аnswers:
#include<iostream.h> #include<stdio.h> class OFFER { int OfferId; char Description[80]; void Show() { cout<<offerld<<":"<<Description<<endl; } public: void Enter() { cin>>offerId; gets(Description): } }; void main() { OFFER Obj; Obj.Enter(); //Obi.Show(); //Show()cannot be called because //it is a private member }
Question 5:
What is the difference between members in private visibility mode and the members in public visibility mode inside a class? Also give a suitable C++ code to illustrate both. Delhi 2012
or
Differentiate between public and private visibility modes in context of object oriented programming using a suitable example. Delhi 2011
Аnswers:
A member declared as private remains hidden from outside world and it can only be accessed by the member function of the class in which it is declared. A member declared as public is made available to the outside world. That is , it can be accessed by any function, any expression in the program but only by using an object of the same class type.
e.g-
class A { private: int x; void show(); public: int y; void get(); };
The members x and show( ) are private and these are not accessible outside the class where the members y and get( ) are public so these are accessible outside the class.
Question 6:
When is public visibility of a mode applied to members of a class? Also give an example to illustrate. Delhi 2011C
Аnswers:
When we need to access the members from out side the class then we apply public mode preceding any members.
e.g.
#include<iostream.h> class ABC { public: int b; void show() { cout<<"b ="<<b; } }; void main() { ABC x; x.b=20; x.show(); }
Question 7:
Rewrite the following C++ program code after removing the syntax error(s) (if any). Underline each correction.
All India 2010
include<iostream.h> class FLIGHT { long FlightCode; char Description[25]; public void Addlnfo() { cin>>FlightCode; gets(Description); } void ShowInfo() { cout<<FlightCode«":" <<Description<<endl; } }; void main() { FLIGHT F; Addlnfo.F(); Showlnfo.F(); }
Аnswers:
# include<iostream.h> #include<stdio.h> class FLIGHT { long FlightCode; char Description[25]; public: void Addlnfo() { cin>>FlightCode; gets(Descri pti on); } void Showlnfo() ( cout<<FlightCode<<":"<<Description<<endl; } }; void main() { FLIGHT F; F.Addlnfo(); F.ShowInfo(); }
Question 8:
Rewrite the following C++ program code after removing the syntax error(s) (if any). Underline each correction.
Delhi 2010
#include<iostream.h> class TRAIN { long TrainNo; char Description[25]; public void Entry() { cin>>TrainNo; gets(Description); } void Display() { cout<<TrainNo<<":"<<Description<<endl; } }; void main( ) { TRAIN T; Entry. T(); Display.T(); }
Аnswers:
#include<iostream.h> #include<stdio.h> class TRAIN { long TrainNo; char Description[25]; public: void Entry() { cin>>TrainNo; gets(Description); } void Display() { cout<<TrainNo<<" : "<<Description<<endl; } }; void main() { TRAIN T; T.Entry(); T.Display(); }
Question 9:
Rewrite the following program after removing the syntactical error(s) (if any) Underline each correction. Delhi 2009C
#include<iostream.h> class Transport { char Model[20]; char Name[20]; void Get() { gets(Model); gets(Name); } void Show() { cout<<Model<<endl; puts(Name); } }; void main( ) { Transport T; T.Get(); Show(); }
Аnswers:
#include<iostream.h> #include<stdio.h> class Transport { char Model[20]; char Name[20]; public: void Get() { gets(Model); gets(Name); } void Show() { cout<<Model<<endl; puts(Name); } }; void main() { Transport T: T.Get(); T.Show(); }
4-5 Marks Questions
Question 10:
Write the definition of a class BOX in C++ with the following description: All India 2017
Аnswer:
class Box { int BoxNumber; float Side, Area; void ExecArea() { Area = Side*Side; } public: void GetBox() { cout<<"Enter the values of BoxNumber and Side”; cin>>BoxNumber; cin>>Side; ExecArea(); } void ShowBox() { cout<<"BoxNumber:"<<BoxNumber<<endl; cout<<”Side:"<<Side<<endl: cout<<”Area:"<<Area; }
Question 11:
Write the definition of a class METROPOLIS in C++ with following description:
Аnswer:
class METROPOLIS { int MCode; char MName[20]; long int MPop; float Area, PopDens; void CalDen() { PopDens = MPop/Area; } public: void Enter() { cout<<"\nEnter the code"; cin>>MCode; cout<<"\nEnter the name"; gets(MName); cout<<"\n Enter the population"; cin>>MPop; cout<<"\n Enter the area coverage"; cin>>Area; CalDen(); } void ViewALL() { cout<<"\nCode: "<<MCode; cout<<"\nName: "<MName; cout<<"\nPopulation:"<<MPop; cout<<"\nArea Coverage : "<<Area ; cout<<"\nPopulation Density :"<<PopDens; if(PopDens>12000)cout<<"Highly Populated Area"; } };
Question 12:
Write the definition of a class CITY in C++ with following description: All India 2016
Аnswer:
class CITY { int Ccode; char CName[20]; long int Pop; float KM, Density; void DenCal() { Density = Pop/KM; } public : void Record() { cout<<"\nEnter the city code" cin<<Ccode; cout<<"\nEnter the city name" gets(CName); cout<<"\nEnter city’s populiation" cin>>Pop; cout<<"\nEnter the Area coverage by city"; cin>>KM; DenCal (); } void View() { cout<<"\nCity code:"<<Ccode; cout<<"\nCity name:"<<CName; cout<<"\nPopul at ion:"<<Pop; cout<<"\nArea coverage:"<<KM; cout<<"\nPopul ation density:"<<Density; if(Density>10000) cout<<"Highly Populated City"; } };
Question 13:
Write the definition of a class PIC in C++ with following description Delhi 2015
Аnswer:
class PIC { int Pno; char Category[20]; char Location[20]; void FixLocation() { if(strcmp(Category,"Antique”)==0) strcpy(Location,"Ustad Khan"); else if(strcmp(Category,"Modern" )==0) strcpy(Location,"Jim Plaq”); else if(strcmp(Category,”Classic”)==0) strcpy(Location,"Amina"); } public: void Enter() { cout<<"\nEnter the picture number"; cin>>Pno; cout<<"\nEnter the category"; gets(Category); FixLocation(); } void SeeAll() { cout<<"\nPicture number:"<<Pno; cout<<"\nCategory: "<<Category; cout<<"\nLocation:"<<Location; } };
Аnswer:
class Photo { int Pno; char Category[20]; char Exhibit[20]; void FixExhibit() { if(strcmp(Category,"Antique")== 0) strcpy(Exhibit, "Zaveri"); else if(strcmp(Category,"Modem") == 0) strcpy(Exhibit, "Johnsen"); else if(strcmp(Category,"Classic") == 0) strcpy(Exhibit, "Terenida”); } public: void Register() { cout<<"\nEnter the photo number"; cin>>Pno; cout<<"\nEnter the category"; gets(Category); FixExhibit(); } void ViewAll() { cout<<"\nPhoto number :"<<Pno; cout<<"\nCategory:"<<Category; cout<<"\nExhibit:”<<Exhibit; } };
- Enroll( ) //A function to allow user to enter values
//SID, Type, Name and call
//AssignPay( ) function - SeeData( ) //A function to display all the data members
Аnswer:
class STAFF { long int SID; char Type; float Pay; char Name[30]; void AssignPay() { if(Type == 'D') Pay = 95000; else if(Type == 'M') Pay = 75000; else if (Type == 'E') Pay = 60000; else if(Type == 'S') Pay = 45000; } public: void Enroll() { cout<<”Enter the values of staff ID, Type, Name"; cin>>SID; cin>>Type; gets(Name); AssignPay(); } void seeData() { cout<<SID<<Type<<Name<<Pay; } };
Question 16:
Define a class Seminar with the following specification private members: Delhi 2013C
Seminarld long
Topic string of 20 characters
VenueLocation string of 20 characters
Fee float
CalcFee( ) function to calculate Fee depending on VenueLocation
Venue Location | Fee |
Outdoor | 5000 |
Indoor Non-AC | 6500 |
Indoor AC | 7500 |
Public members
Register( ) function to accept values for Seminarld, Topic, VenueLocation and call CalcFee( ) to calculate Fee.
ViewSeminarf( ) function to display all the data members on the screen.
Аnswer:
class Seminar { long SeminarId; char Topic[20]. VenueLocation[20]; float Fee; void CalcFee() { if(strcmp(VenueFocation,"Outdoor")==0) Fee=5000; else if(strcmp(Venuetocation,"Indoor Non-AC")==0) Fee=6500; else if(strcmp(VenueFocation,"Indoor AC")==0) Fee=7500; } public: void Register() { cout<<"Enter Seminar Id"; cin>>SeminarId; cout<<"Enter Topic"; gets(Topic); cout<<"Enter Venue Focation"; gets(VenueLocation); CalcFee(); } void ViewSeminar() { cout<<"SeminarId:"<<SeminarId<<endl; cout<<"Topic: "<<Topic<<endl; cout<<"Venue Location;"<<VenueFocation<<endl; cout<<"Fee is:"<<Fee; } };
Question 17:
Define a class RESTRA in C++ with following description: Delhi 2012
Private members
- FoodCode of type int
- Food of type string
- FType of type string
- Sticker of type string
- A member function GetStickerf( ) to assign the following values for Food Sticker as per the given FType
FType Sticker
Vegetarian GREEN
Contains Egg YELLOW
Non-Vegetarian RED
Public members
- A function GetFood( ) to allow user to enter values for FoodCode, Food, FType and call function GetSticker( ) to assign Sticker.
- A function ShowFoodf( ) to allow user to view the content of all the data members.
Аnswer:
class RESTRA { int FoodCode; char Food[30], FType[20],Sticker[20]; void GetSticker() { if(strcmp(FType,"Vegetari an”)==0) strcpy(Sticker,"GREEN"); else if(strcmp(FType,"Contains Egg")==0) strcpy(Sticker,"YELLOW"); else if(strcmpCFType,"Non-Vegetarian")==0) strcpy(Sticker,"RED”); } public: void GetFood() { cout<<"Enter FoodCode, Food, Food Type"; cin>>FoodCode; gets(Food); gets(FType); GetSticker(); } void ShowFood() { cout<<"Entered values of FoodCode, Food, FoodType,Sticker"; cout<<FoodCode<<Food<<FType<<Sticker; } };
Question 18:
Define a class SUPPLY in C++ with following description: All Indio 2012
Private members
- Code of type int
- FoodName of type string
- Sticker of type string
- FoodType of type string
- A member function GetTypef ( ) to assign the following values for FoodType as per the given Sticker
Sticker FoodType
GREEN Vegetarian
YELLOW Contains Egg
RED Non-Vegetarian
Public members
- A function FoodIn( ) to allow user to enter values for Code, FoodName, Sticker and call function GetType( ) to assign respective FoodType.
- A function FoodOut( ) to allow user to view the content of all the data members.
Аnswer:
class SUPPLY { int Code; char FoodName[30],Sticker[30]; char FoodType[20]; void GetType() { if(strcmp(Sticker, "GREEN”)==0) strcpy(FoodType,"Vegetarian" ); else if(strcmp(Sticker,"YELLOW")==0) strcpy(FoodType,"Contains Egg"); else if(strcmp(Sticker,"RED")==0) strcpy(FoodType,"Non -Vegeta rian"); } public: void FoodIn() { cout<<"Enter Code, FoodName, Sticker"; cin>>Code; gets(FoodName); gets(Sticker); GetType(); } void Food0ut() { cout<<"Code,FoodName,Sticker,FoodType are"; cout<<Code<<FoodName<<Sticker<<FoodType; } };
Question 19:
Define a class Candidate in C++with following description: Delhi 2o11
Private members
- A data member RNo (Registration Number) of type long
- A data member Name of type string
- A data member Score of type float
- A data member Remarks of type string
- A member function AssignRem( ) to assign the remarks as per the score obtained by a candidate.
- Score range and the respective remarks are shown as follow:
Score Remarks
>=50 Selected
less than 50 Not Selected
Public members
- A function ENTER( ) to .allow user to enter values for RNo, Name, .Score and call function AssignRem( ) to assign Remarks.
- A function DISPLAY( ) to allow user to view the content of all data members.
Аnswer:
class Candidate { long RNo; char Name[40]; float Score; char Remarks[20]; void AssignRem() { if(Score>=50) strcpy(Remarks,"Selected"); else strcpy(Remarks,"Not Selected"); } public: void ENTER() { cout<<"Enter the values for Rno,Name,Score”; cin>>RNo; gets(Name); cin>>Score; AssignRem(); } void DISPLAY() { cout<<RNo<<Name<<Score<<Remarks; } };
Question 20:
Define a class Applicant in C++ with following description: All IndiA 2011
Private members
- A data member ANo (Admission Number) of type long
- A data member Name of type string
- A data member Agg (Aggregate Marks) of type float
- A data member Grade of type char
- A member function GradeMe( ) to find the grade as per the aggregate marks obtained by a student. Equivalent aggregate marks range and the respective grades are shown as follow:
Aggregate marks Grade
>=80 A
less than 80 and >=65 B
less than 65 and >=50 C
less than 50 D
Public members
- A function ENTER( ) to allow user to enter values for ANo, Name, Agg and call function GradeMe( ) to find the Grade.
- A function RESULT( ) to allow user to view the content of all data members.
Аnswer:
class Applicant { long ANo; char Name[40]; float Agg; char Grade; void GradeMe() { if(Agg>=80) Grade='A'; else if(Agg<80 && Agg>=65) Grade='B'; else if(Agg<65 && Agg>=50) Grade='C'; else Grade='D'; public: void ENTER() { cout<<"Enter the values for Ano.Name.Agg" cin>>ANo; gets(Name); cin>>Agg; GradeMe(); } void RESULT() { cout<<ANo<<Name<<Agg<<Grade; } };
Question 21:
Define a class ITEM in C++ with the following specification: Delhi 2010
Private members
- Code of type integer (Item Code)
- Iname of type string (Item Name)
- Price of type float(Price of each item)
- Qty of type integer(Quantity of item stock)
- Offer of type float(Offer percentage on the item)
- A member function GetOffer( ) to calculate offer percentage as follows:
If Qty <=50 Offer is 0
If 50<Qtv<=l 00 Offer is 5
If Qty >100 Offer is 10
Public members
- A function GetStock( ) to allow user to enter values for Code, Iname, Price, Qty and call function GetOffer( ) to calculate offer.
- A function ShowItem( ) to allow user to view the content of all data members.
Аnswer:
class ITEM { int Code; char Iname[20]: float Price; int Qty; float Offer; void GetOffer() { if(Qty<=50) Offer=0; else if(Qty<=100) Offer=5; else Offer=10; } public: void GetStock() { cout<<"Enter the values for code, Item name, Price, Quantity"; cin>>Code; gets(Iname); cin>>Price>>Qty; GetOffer(); } void ShowItem() { cout<<Code<<Iname<<Price<<Qty<<Offer; } };
Question 22:
Define a class STOCK in C++ with the following specification: All India 2010
Private members
- ICode of type integer (Item Code)
- Item of type string (Item Name)
- Price of type float(Price of each item)
- Qty of type integer(Quantity in stock)
- Discount of type float(Discount percentage on the item)
- A member function FindDisc( ) to calculate discount percentage as follows:
If Qty <=50 Discount is 0
If 50<Qty<=100 Discount is 5
If Qty >100 Discount is 10 Public members
- A function Buy( ) to allow user to enter values for ICode, Item, Price, Qty and call function FindDisc( ) to calculate discount.
- A function ShowAll( ) to allow user to view the content of all data members.
Аnswer:
class STOCK { int ICode; char Item[20]: float Price: int Qty; float Discount: void FindDisc(): { if(Qty<=50) Discount=0; else if(Qty<=100) Discount=5; else Discount=10; } public: void Buy() { cout<<"Enter the values for Icode,Item,Price,Qty"; cin>>ICode; gets(Item); cin>>Price>>Qty; FindDisc(); } void ShowAll() { cout<<ICode<<Item<<Price<<Qty<<Discount; } };
Question 23:
Define a class RESORT in C++ with the following specification: Delhi 2009
Private members
- Rno – Data member to store room number
- Name – Data member to store customer name
- Charges – Data member to store per day charges
- Days – Data member to store number of days of stay
- COMPUTEO – Function to calculate and return amount as days*charges and if the value of days *charges is more than 11000, then as 1.02*Days *Charges
Public members
- Getinfo( ) Function tfo enter the content Rno, Name, Charges and Days
- Dispinfo( ) Function to display Rno, Name, Charges, Days and Amount (amount to be displayed by calling function COMPUTE( ))
Аnswer:
class RESORT { int Rno; char Name[30]; float Charges; int Days ; float COMPUTE() { float temp=Days*Charges; if(temp>11000) return(1.02*temp); return temp; } public: void Getinfo() { cout<<"enter the room number:"; cin>>Rno; cout<<"enter the customer name:"; gets(Name); cout<<”enter the room charges per day"; cin>>Charges; cout<<"enter number of days stayed by customer:"; cin>>Days: } void Dispinfo() { cout<<"Room number:"<<Rno; cout<<"Customer Name:"; puts(Name) ; cout<<"Charges per day:"<<Charges; cout<<"Number of days stayed by customer:"<<Days; cout<<"Total charges of customer: "<<COMPUTE(); } };
Question 24:
Define a class HOTEL in C++ with the following specification: All IndiA 2009
Private members
- Rno Data member to store room number
- Name Data member to store customer name
- Tariff Data member to store per day charges
- NOD Data member to store number of days of stay
- CALC( ) Function to calculate and return amount as NOD*Tariff and if the value of NOD*Tariff is more than 10000, then as 1.05 NOD *charges
Public members
- Checkin( ) Function to enter the content Rno, Name, Tariff and NOD
- Checkout( ) Function to display Rno, Name, Tariff, NOD and Amount (amount to be displayed by calling function
CALC( ))
Аnswer:
class HOTEL { int Rno,NOD; char Name[30]; float Tariff; float CALC() { float temp=NOD*Tariff; if(temp>10000) return(1.05*temp); return temp; } public: void Checkin() { cout«"Enter the room number:"; cin>>Rno; cout<<"Enter the customer name:"; gets(Name); cout<<"Enter the room charges per day"; cin>>Tariff ; cout<<"Enter number of days stayed by customer:"; cin>>NOD; } void Checkout() { cout<<"Room number:"<<Rno; cout<<"Customer Name:"; puts(Name); cout<<"Charges per day:"<<Tariff; cout<<"Number of days stayed by customer:"<<NOD; cout<<"Total charges of customer:"<<CALC(); } };
Discover more from EduGrown School
Subscribe to get the latest posts sent to your email.