Quantcast
Channel: Learn CBSE
Viewing all articles
Browse latest Browse all 9061

Important Questions for Class 12 Computer Science (C++) – Classes and Objects

$
0
0

Important Questions for Class 12 Computer Science (C++) – Classes and Objects

Previous years Examination Questions
2 Marks Questions

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 Gl1, 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:

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:

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:

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:

4-5 Marks Questions

Question 10:
Write the definition of a class BOX in C++ with the following description: All India 2017
important-questions-for-class-12-computer-science-c-classes-and-objects-(160-1)
Аnswer:

 

Question 11:
Write the definition of a class METROPOLIS in C++ with following description: Delhi 2016
important-questions-for-class-12-computer-science-c-classes-and-objects-(160-2)
Аnswer:

 

Question 12:
Write the definition of a class CITY in C++ with following description: All India 2016
important-questions-for-class-12-computer-science-c-classes-and-objects-(160-3)
Аnswer:

 

Question 13:
Write the definition of a class PIC in C++ with following description Delhi 2015
important-questions-for-class-12-computer-science-c-classes-and-objects-(161-1)

Аnswer:

 

Question 14:
Write the definition of a class Photo in C++ with following description: All Indio 2015
important-questions-for-class-12-computer-science-c-classes-and-objects-(161-2)

Аnswer:

 

Question 15:
Write the definition of a class STAFF in C++ with following description: All India 20isc
important-questions-for-class-12-computer-science-c-classes-and-objects-(161-3)
Public Members

  • 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:

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

VenueLocation 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:

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:

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:

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:

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:

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:

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:

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:

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:

The post Important Questions for Class 12 Computer Science (C++) – Classes and Objects appeared first on Learn CBSE.


Viewing all articles
Browse latest Browse all 9061

Trending Articles