a) Can we declare base class constructor or destructor as virtual if yes then why we want to do?
b) How we can declare a class as abstract and why we need abstract classes?
c) Why we cannot able to create objects of abstract classes give reasonable justification of that.
Write output of given code segments and explain it logically, answer without explanation will be awarded zero marks.
a)
#include<iostream>
using namespace std;
class Base
{
public:
virtual void show() { cout<<" In Base \n"; }
};
class Derived: public Base
{
public:
void show() { cout<<"In Derived \n"; }
};
int main(void)
{
Base *bp = new Derived;
bp->show();
Base &br = *bp;
br.show();
return 0;
}
Answer the following short questions. Your answer should be precise.
a) Can we declare base class constructor or destructor as virtual if yes then why we want to do?
b) How we can declare a class as abstract and why we need abstract classes?
c) Why we cannot able to create objects of abstract classes give reasonable justification of that.
Create a class template for a class named GeneralStackthat holds
In the main() function, create three objects with different data types of class General Stack and test the functionality of member functions for various values of data members for these objects.
Create a class called Transmitter. It has three unsigned int private data members: pay_load,
parity and data_packet; It has one function called parity_bit(). It calculates parity bit by XORing
all pay_load bits and assign the value to parity. By using function get pay_load(), pay_load is got.
data_packet is calculated by multiplying parity_bit with 32768 and add the product with (pay_load
– 32768). Create another class called Receiver. It has unsigned int private data members Rx_Pkt
, Rx_Data and E_Flag. Rx_Data is got from Rx_Data as given below. First E_Flag is calculated
from Rx_Pkt by XORing all bits. If E_Flag is zero, then Rx_Data = Rx_Pkt – 32768, Else a
message should warn about corrupted packet. Derive a class called Tranceiver from the above two
classes and check the functionalities. Use any other functions if needed.
Write a program that has a class Train with data members seats_first_class, seats_second_class
and seats_ac_2tier and member functions to set and display data. Derive a class Reservation that has
data members booked_first_class, booked_second_class and booked_ac_2tier and functions to book
and cancel tickets and display status.
Write a program for a publishing company that markets both printed books and audio visual
lectures stored on CDs. Write a class Publication thatstores title and price. Derive a class book which
has an additional member as no_pages and a class Lecture with member play_time.
Consider the following inheritance hierarchy:
class A{
protected:
int x, y;
public:
int z;
};
class B: private A{
private:
int a, b, c;
public:
};
Int main(){
Aobja;
void set(int x, int y, int z, int a, int b, int c);
B objb;
}
a) How many data members does B have? Write their names.
b) How many of B’s data members are visible in B? Write down their names.
c) Which members of B are accessible in main()? How will they be accessed?
d) If the protected Access specifier in A is changed to public, then how many members of B will be
accessed in main() and how?
e) Define the function set() without changing its signature as given above.
f) Write a default constructor for the class B that does not have an empty parameter list.
g) If the data members of A become private, then how they be initialized?
h) Add a static data member in class B. Can we use this pointer with static members? If yes how?
Write a program of c++ with a following statement: A person can be an employee or a student. An employee may have rights of admin officer or of
academic officer. These class hierarchies represent multi-level inheritance. However, a Dean or
Head of Department(HOD) may have rights to modify the status already defined by an administrator
academic officer. Implement all these classes with appropriate data members and proper suitable
functions and within the main function, create instances of all classes and test the described working
of all these classes.
Design a class that has an array of floating-point numbers. The constructor should accept an
integer argument and dynamically allocate the array to hold that many numbers. The
destructor should free the memory held by the array. In addition, there should be member
functions to perform the following operations:
• Store a number at any index of the array
• Retrieve a number from any index of the array.
Return the highest value stored in the array
• Return the lowest value stored in the array
• Return the average of all the numbers stored in the array.