• No results found

Hybrid Inheritance: - This is a Mixture of two or More Inheritance and in this Inheritance a Code May Contains two or Three types of inheritance in

In document Object Oriented Technology (Page 39-56)

Class and Objects

Q.11. What is inheritance?

5) Hybrid Inheritance: - This is a Mixture of two or More Inheritance and in this Inheritance a Code May Contains two or Three types of inheritance in

Single Code.

Q13 What is a virtual base class? -

Ans When two or more objects are derived from a common base class, we can prevent multiple copies of the base class being present in an object derived from those objects by declaring the base class as virtual when it is being inherited. Such a base class is known as virtual base class. This can be achieved by preceding the base class‟ name with the word virtual.

Consider following example:

class A {

public:

int i;

};

class B : virtual public A {

public:

int j;

};

class C: virtual public A {

public:

int k;

};

class D: public B, public C {

public:

int sum;

};

main() {

D ob;

ob.i = 10; //unambiguous since only one copy of i is inherited.

ob.j = 20;

ob.k = 30;

ob.sum = ob.i + ob.j + ob.k;

cout << “Value of i is : ”<< ob.i<<”\n”;

cout << “Value of j is : ”<< ob.j<<”\n”; cout << “Value of k is :”<< ob.k<<”\n”;

cout << “Sum is : ”<< ob.sum <<”\n”;

return 0;

}.

Q14 What are the types to access member?

Ans. Access Control

A derived class can access all the non-private members of its base class.

Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.

We can summarize the different access types according to who can access them in the following way:

Access public protected private

Same class yes yes yes

Derived classes yes yes no

Outside classes yes no no

A derived class inherits all base class methods with the following exceptions:

Constructors, destructors and copy constructors of the base class.

Overloaded operators of the base class.

The friend functions of the base class.

Q.1 The process of building new classes from existing one is called ______.

(A) Polymorphism (B) Structure

(C) Inheritance (D) Cascading Ans: C

Q.2 If a class C is derived from class B, which is derived from class A, all through public inheritance, then a class C member function can access (A) protected and public data only in C and B.

(B) protected and public data only in C.

(C) rivate data in A and B.

(D) protected data in A and B. Ans:D

Q.3 Usually a pure virtual function

(A) has complete function body. (B) will never be called.

(C) will be called only to delete an object.

(D) is defined only in derived class. Ans:D

Q4 Number of constructor in a class can be:

(a) minimum one (b) zero

(c) two (d) as many as required Ans:d

Q5 Name of a constructor is:

(a) user defined (b) same as class name

(c) same as program file name (d) none of the above Ans:b Q6. Constructor is involved :

(a) when class object is created (b) when class object is initialized (c) through an explicit call (d) none of the above Ans:a Q7. The number of copies created for a static data member of a class, when 10

class object are created would be:

(a) 0 (b) 1

(c) 2 (d) 4 Ans:B

Q8. A destructor :

(a) has a return type (b) may take parameters

(c) has same name of class (d) both b and a Ans:C

Q9. A constructor:

(a) has a return type (b) may take parameter

(c) has same name as class (d) both b and c Ans:D Q10. The member of a class by default are:

(a) Private (b) Protected

(c) Public (d) no default exists Ans:A

Q11 A new class can be derived from an existing class. This concept is called us:

(a) inheritance (b) polymorphism

(c) overloading (d) dynamic binding Ans:A

Q12 A C + + Class can hold?

(a) only data (b) only function

(c) both data and function (d) none of the above Ans:C

Q13 Among the following, which one is the scope resolution operator?

(a) * (b)

(c) : : (d) ! Ans:C

Q14. Cout is:

(a) a key word (b) an obect

(c) a library function (d) a variable Ans:B

Q15. The pure virtual functions are defined in:

(a) base class (b) drived class

(c) main program (d) both a and b

Ans:B

Q16. Inheritance is a way to:

(a) make new classes

(b) pass arguments of objects of class

(c) add feature to existing classes without rewriting them

(d) Improve to existing classes without rewriting them Ans:C

Q17. By default, a class members are:

(a) Public (b) Private

(c) Protected (d) Any of the above Ans:B

Q18. Friend keyword can be used for :

(a) a function (b) a class

(c) both function and class (d) a data member Ans:C

Q19 Inline functions ………..call overload:

(a) Increase (b) Reduce

(c) Depends on situation (d) None Ans:B

Chapter -3

Polymorphism

Q1 What is function overloading in C++?

Ans. Function overloading: A feature in C++ that enables several functions of the same name can be defined with different types of parameters or different number of parameters. This feature is called function overloading. The appropriate function will be identified by the compiler by examining the number or the types of parameters / arguments in the overloaded function. Function overloading reduces the investment of different function names and used to perform similar functionality by more than one function.

Consider a function print, which displays an int. As shown in the following example, you can overload the function print to display other types, for example, double and char*. You can have three functions with the same name, each performing a similar operation on a different data type:

#include <iostream>

using namespace std;

void print(int i) {

cout << " Here is int " << i << endl;

}

void print(double f) {

cout << " Here is float " << f << endl;

}

void print(char* c) {

cout << " Here is char* " << c << endl;

}

int main() { print(10);

print(10.10);

print("ten");

} Result is

Here is int 10 Here is float 10.1 Here is char* ten

Q2 What is operator overloading in C++?

Ans In C++ the overloading principle applies not only to functions, but to operators too. That is, of operators can be extended to work not just with built-in types but also classes. A programmer can provide his or her own operator to a class by overloading the built-in operator to perform some specific computation when the operator is used on objects of that class.

// This example illustrates overloading the plus (+) operator.

#include <iostream>

using namespace std;

class complx {

double real, imag;

public:

complx( double real = 0., double imag = 0.); // constructor complx operator+(const complx&) const; // operator+() };

// define constructor

complx::complx( double r, double i ) {

real = r; imag = i;

}

// define overloaded + (plus) operator

complx complx::operator+ (const complx& c) const {

complx result;

result.real = (this->real + c.real);

Q3 What do you mean by early binding and late binding?

Ans. Early Binding: Events occurring at compile time are known as early binding. In the process of early binding all info which is required for a function call is known at compile time. Early binding is a fast and efficient process. Examples of early binding: function calls, overloaded function calls, and overloaded operators.

Late Binding: In this process all info which is required for a function call is not known at compile time. Hence, objects and functions are not linked at run time. Late Binding is a slow process. However, it provides flexibility to the code. Example of Late Binding: Virtual functions.

Q4 What do you mean by Virtual Function:

Ans A virtual function is a function in a base class that is declared using the keyword virtual. Defining in a base class a virtual function, with another version in a derived class, signals to the compiler that we don't want static linkage for this function.

What we do want is the selection of the function to be called at any given point in the program to be based on the kind of object for which it is called. This sort of operation is referred to as dynamic linkage, or late binding.

Q5 What do you mean by Pure Virtual Functions:

Ans. It's possible that you'd want to include a virtual function in a base class so that it may be redefined in a derived class to suit the objects of that class, but that there is no meaningful definition you could give for the function in the base class.

We can change the virtual function area() in the base class to the following:

class Shape { protected:

int width, height;

public:

The = 0 tells the compiler that the function has no body and above virtual function will be called pure virtual function.

Q6 Explain Unary and binary operator overloading.

Ans The unary operators operate on a single operand and following are the examples of Uninary operators:

The increment (++) and decrement (--) operators.

The unary minus (-) operator.

The logical not (!) operator.

The unary operators operate on the object for which they were called and normally, this operator appears on the left side of the object, as in !obj, -obj, and ++obj but sometime they can be used as postfix as well like obj++

or obj--.

Following example explain how minus (-) operator can be overloaded for prefix as well as postfix usage.

#include <iostream>

inches = i;

Hope above example makes your concept clear and you can apply similar concept to overload Logical Not Operators (!).

Binary operators

The Binary operators take two arguments and following are the examples of Binary operators. You use binary operators very frequently like addition (+) operator, subtraction (-) operator and division (/) operator.

Following example explain how addition (+) operator can be overloaded.

Similar way you can overload subtraction (-) and division (/) operators.

#include <iostream>

using namespace std;

double getVolume(void) {

// Main function for the program int main( )

{

Box Box1; // Declare Box1 of type Box Box Box2; // Declare Box2 of type Box Box Box3; // Declare Box3 of type Box

double volume = 0.0; // Store the volume of a box here

// box 1 specification Box1.setLength(6.0);

Box1.setBreadth(7.0);

Box1.setHeight(5.0);

// box 2 specification Box2.setLength(12.0);

Box2.setBreadth(13.0);

Box2.setHeight(10.0);

// volume of box 1

volume = Box1.getVolume();

cout << "Volume of Box1 : " << volume <<endl;

// volume of box 2

volume = Box2.getVolume();

cout << "Volume of Box2 : " << volume <<endl;

cout << "Volume of Box3 : " << volume <<endl;

Volume of Box3 : 5400

Q7 What is assignment operator overloading ?

Ans. We can overload the assignment operator (=) just as you can other operators and it can be used to create an object just like the copy constructor.

Following example explain how an assignment operator can be overloaded.

#include <iostream>

D1.displayDistance();

cout << "Second Distance :";

D2.displayDistance();

// use assignment operator D1 = D2;

cout << "First Distance :";

D1.displayDistance();

return 0;

}

When the above code is compiled and executed, it produces following result:

First Distance : F: 11 I:10 Second Distance :F: 5 I:11 First Distance :F: 5 I:11 Q8 What is free store in C++?

Ans. The free store is one of the two dynamic memory areas, allocated/freed by new/delete. Object lifetime can be less than the time the storage is allocated; that is, free store objects can have memory allocated without being immediately initialized, and can be destroyed without the memory being immediately deallocated. During the period when the storage is allocated but outside the object's lifetime, the storage may be accessed and manipulated through a void* but none of the proto-object's nonstatic members or member functions may be accessed, have their addresses taken, or be otherwise manipulated.

Q9 Explain Abstract classes.

Ans Abstract classes

An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier (= 0) in the declaration of a virtual member function in the class declaration.

The following is an example of an abstract class:

class AB { public:

virtual void f() = 0;

};

Function AB::f is a pure virtual function. A function declaration cannot have both a pure specifier and a definition. For example, the compiler will not allow the following:

struct A {

virtual void g() { } = 0;

};

You cannot use an abstract class as a parameter type, a function return type, or the type of an explicit conversion, nor can you declare an object of an abstract class. You can, however, declare pointers and references to an abstract class. The following example demonstrates this:

struct A {

virtual void f() = 0;

};

struct B : A { virtual void f() { } };

// Error:

// Class A is an abstract class // A g();

// Error:

// Class A is an abstract class // void h(A);

A& i(A&);

int main() { // Error:

// Class A is an abstract class // A a;

A* pa;

B b;

// Error:

// Class A is an abstract class // static_cast<A>(b);

}

Class A is an abstract class. The compiler would not allow the function declarations A g() or void h(A), declaration of object a, nor the static cast of b to type A.

Virtual member functions are inherited. A class derived from an abstract base class will also be abstract unless you override each pure virtual function in the derived class.

For example:

class AB { public:

virtual void f() = 0;

};

class D2 : public AB { void g();

};

int main() { D2 d;

}

The compiler will not allow the declaration of object d because D2 is an abstract class; it inherited the pure virtual function f()from AB. The compiler will allow the declaration of object d if you define function D2::g().

Note that you can derive an abstract class from a nonabstract class, and you can override a non-pure virtual function with a pure virtual function.

You can call member functions from a constructor or destructor of an abstract class. However, the results of calling (directly or indirectly) a pure virtual function from its constructor are undefined. The following example demonstrates this:

struct A { A() { direct();

indirect();

}

virtual void direct() = 0;

virtual void indirect() { direct(); } };

The default constructor of A calls the pure virtual function direct() both directly and indirectly (through indirect()).

The compiler issues a warning for the direct call to the pure virtual function, but not for the indirect call.

Q.1 Overloading the function operator

(A) requires a class with an overloaded operator.

(B) requires a class with an overloaded [ ] operator.

(C) allows you to create objects that act syntactically like functions.

(D) usually make use of a constructor that takes arguments. Ans:A

Q2. For operator overloading the operands should be of type:

(a) Minimum one (b) zero

(c) user defined (d) any type Ans:c

Q3. The word which makes the name of tan operator overloading function is:

(a) the operator symbol (b) the key word operator (c) the keyboard operator followed by the operator symbol

(d) user defined Ans:C

Q4. The operator over loading function can be :

(a) member function only (b) non member function only

(c) both a or b (d) none of the above Ans.C

Q5. Templates can be used:

(a) Functions only (b) classes only

(c) functions and classes both (d) none of the above Ans:C Q6. Which of the following operators can be over-loaded?

(a) ≫ (b) ? :

(c) both a and b (d) no such operator exists Ans:A Q7 Ability to take many forms is………..

(a) Polymorphism (b) Encapsulation

(c) Member function (d) Inheritance Ans:A

Chapter -4

In document Object Oriented Technology (Page 39-56)