Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

C++ Chpater 2

Q.  How does a C++ structure differ from a C++ class?

Ans.  Class is the most important feature of C++. The concept of class is derived from the concept of structure used in C or a class is an extension of the idea of structure used in C. Both are use for creating and implement the used define data type.  Class is a new way of creating and implementing a user-define data type.
Structure provides a mechanism for packing the different data types together. In C++ structure support the all features of C structure and additionally has many new features which support the OOP concepts and attempt to bring the user-define data type as possible to the built-in data types and also provide a facility to hide the data which is one of the main principles of OOP. Such as that C++ structure allow programmer to both declare and define the variables and functions. Data members and data functions can be make private or public in C++ structure, these features are not available in C structure.
A class is new way to define the used data type. It allow to bind the data and its associated functions together. A class also allows hiding the data and functions from external use if necessary. A class create a new ‘abstract data type’ that can be treated like any other built-in data type. Generally a class has two parts class declaration and class function definition.

The differences between structure and class in C++ are given below:-

A structure is defined with ‘struct’ keyword. But a class is defined with keyword ‘class’. Ex.

struct book

class demo

Another difference between structure and class is that, by default members of structure are ‘public’ but by default members of class are ‘private’.

Q.  What is Polymorphism? What are the different types of polymorphism? Explain each.
Explain the compile-time and run-time polymorphism with suitable examples.

Ans. Polymorphism means one name having multiple forms. Polymorphism is one of the most important characteristic of OOP. Concept of ‘polymorphism’ is used in function overloading and in operator overloading. There are two types of Polymorphism ‘Compile Time’ and ‘Run Time’.
Compile Time Polymorphism
When the member function is selected for calling/invoking by matching the arguments, on the basis of number and types is called ‘compile time polymorphism’. The information of arguments is passes to compiler on which base compiler choose the appropriate function for calling. This process is also known as ‘early binding’ or ‘static binding’ or ‘static linking’ and also called ‘compile time polymorphism’. ‘Early binding’ means that an object is bound to its function call at compile time.

Run Time Polymorphism
When the appropriate member function is selected at the run time it is called ‘Run Time Polymorphism’. To achieve the ‘run time polymorphism’ C++ use the concept of ‘virtual function’. At run time, when it is known what class objects are under consideration, the appropriate version of the function is called. This process is also known as ‘late binding’ because the function is linked with a particular class much later after the compilation. In this case selection of appropriate function is done dynamically at the run time so it is also known as dynamic binding. Dynamic binding is one of the powerful features of C++ and this requires the pointers to objects.

Q. Write a function template for finding the maximum value contained in an array.


class array
int no[100],n,max,i;
void find()
cout<<" Enter Length of Array = ";
cout<<"\n Enter "<<n<<" Elements in Array :: ";
cout<<"\n Maximum Value in Array :: "<<max;
void main()
array obj;

Q. Explain how inheritance aids reusability. 

Ans. Inheritance is the one of most feature of OOP, inheritance allows that object of one class can acquire the properties of objects of another class. Inheritance support the hierarchical classification for ex. ‘Maruti800’ is the part of class ‘car’ which is again part of class ‘vehicle’. The principle behind which inheritance work is that each class share common characteristics with the class from which it is derived. In OOP, the concept of inheritance provides the idea of ‘reusability’, means we can add additional features in existing class without modifying it. This is possible by creating a new derived class from an existing class, which (new class) will have the combined features of both the classes. Inheritance allows the programmer to reuse an existing class, without creating it again.
            Inheritance allows reuse something that already exists rather to create same all over again. For ex. reuse of a class that has already been tested, debugged and used many times can save us the effort of developing and testing the same again. C++ support the concept of inheritance means we can use the some or all features of pre existing class that is already tested and debugged. To creating a new class from the help of an old class is called inheritance. The old class is known as ‘Base’ class and new one is called derived class of subclass. A derived class can share the all or some features/traits from the base class. A class can also inherit the properties from one class of more that one classes/level. There are five types of inheritance:-
  1. Single Inheritance: - A derived class with single base class is called single inheritance.

3. Hierarchical Inheritance: - The features of one class can be inherited by more than one class is called hierarchical inheritance.

4. Multilevel Inheritance: - The concept of deriving a class from another ‘derived class’ is called multilevel inheritance.

5. Hybrid Inheritance: - Hybrid inheritance is the combination of two or more inheritances.

No comments:

Post a Comment