Site icon HTOLOCK

What is the use of this Pointer?

The member function of every object that has access to a pointer is called this, which pointer to the object itself. When a member function is called, it comes into existence with the value into existence with of this set to the address of the object.

Example:

Consider the following class:

Class example

{

private:

int x;

public:

void said data(int x)

{

this x=x;

}

};

In the above example, this x is used to denote the member variable x of the class example. Is the member function said data x=x;

Statement is used then the compiler cannot distinguish which variable is the member. To overcome this situation this pointer is used.

Visibility label:

Private :

When the member of a class is denoted by private visibility labels then that member can not be derived from any drive class. The member is only accessed by the class where it is defined.

Public:

When the member of a class is denoted by public visibility labels, then any class can be derived or access the member of the class or the program.

Protected:

The protected member of a class can be derived by only the immediate derived class.

What is inheritance?

Inheritance is a mechanism of deriving a new class from a base class. There are 4 types of inheritance machanisms-

  1. Single inheritance:

When a derive class is created from a single base class then it is called single inheritance.

Ex: Class base

{

int a;

public c:

int b;

void get data ();

};

class-derived public base

{

int c;

public:

void get c();

void display ();

};

In the above example, the derive class inherited the member variable b and the member function void gets from the base class

2. Multiple inheritance:

When a derived class is created from multiple base classes then it is called multiple inheritance.

Class base1

{

int a;

public:

Int b;

void get data ();

};

class base2

{

int c;

public c:

int d;

void getdata2();

};

class derived: public base1, public2

{

public:

void display();

};

In the above example, the drive class will inherit the public member of base class 1 and base class 2.

3. Multilevel Inheritance:

When a derived class is created from a base class which is created deriving from another base class and so on maybe then it is called multilevel inheritance.

EX: Class base 1

{

public:

int a;

};

class base 2: public base 1

{

public:

int b;

};

class derived: public base 2

{

int z;

public

void get data ();

void display();

};

4. Hierarchical Inheritance:

When a base class is derived by more than one derived class then it is called hierarchical inheritance.

Ex: class base

{

public:

int x,y;

};

class derive 1: public base

{

int a;

public:

void display1();

};

class derive 2: public base

{

int b;

public:

void display 2();

};

5. Hybrid Inheritance:

When a derived class is generated from a single base class or multiple base class and that base class is also then that mechanism is called hybrid Inheritance.

Ex: Class mam base

{

public:

int a;

};

class derive 1: public main base

{

public:

int b;

};

class derive 2: public main base

{

public:

int c;

};

class main drived: public derive 1, public derive 2

{

int d;

public:

void get data ();

void display();

};

Write a short note on private, public, and protected members of a class

These private, public, and protected keywords are known as visibility labels of a class.

Private:

When the member of a class is denoted by private visibility labels then that member can not be derived from any drive class. The member is only accessed by the class where it is defined.

Public:

When the member of a class is denoted by public visibility labels, then any class can be derived or access the member of the class or the program.

Protected:

The protected member of a class can be derived by only the immediate derived class. Multi-level inheritance is not possible.

What is a virtual base class?

Consider the following situation where there is only one parent class called the base and two classes derive from it, derive one, and derive two again a class child derived from derive on and derive two.

In the above situation the child class inheritance the call public and protected member of base class twice. This means the child would have duplicate sets of members from the base class. It produces ambiguity and is avoided by making the common base class a virtual base class.

Ex: class base

{———–};

class derived 1 : virtual public base

{————-};

class derive 2 : public virtual base

{——————};

class child : public derive 1, derive 2

{—————-};

When a class is made a virtual base class c++ compiler takes necessary care to see that only one copy of that class is inheritance paths exist between the virtual base class and child class.

What is an abstract class?

An abstract class is not used to create any object. An abstract class is designed to act as a base class. It is a design concept in program development and provides a base upon which other closes may be built.

Ex: class geometry

{

public:

float perimeter is a;

};

class rectangle: public geometry

{

public:

void draw ( int x, int y)

{—————-}

};

class circle: public geometry

{

public:

void draw(int r)

{————-}

}

Here the geometry class acts as an abstract class.

What is the implementation of the following two definitions:

A> class A: public B, public C {//…..};

B> class A: public C, public B {//…..};

A> This statement implements multiple inheritance, where the class A is created by inheriting from two base classes B and C.

B> This statement implements multiple inheritance, where the class A is created by inheriting from two base classes B and C.

How do the properties of the following derived classes deffer:

A> class D1: private B {//…..};

B> class D2 : public B {//…..};

A> In the first statement the class D1 inherited the protected member and the public member of the base class B and all the inherited members are considered as private members in the derived class D1.

B> The class D2 inherited the protected member of the base class band the members are treated as protected in the derived class D2 and also public members are inherited from the base class B into derived class d2 and the members are treated as public members.

When do we make a class virtual?

When a derived class inheritance the members from multiple base classes and that base class inherits from the single base class and that time that single base class will be treated as a virtual base class.

What is pure virtual function?

A pure virtual function is a function declared in a base class that has no definition relative to the base class. In such a class, the compiler requires each derived class to either define the function or redeclare it as a pure virtual function function. A class containing pure virtual function can not be used to declare any object of its own, since to class acts as to abstruct base class.

Ex: class geometry

{

float area;

virtual float cat area();

}

What are the implications of making a function virtual?

In normal practice to declare a function virtual inside a base class and re-define it in the derived class is usually used. The function inside the base class is seldom used for performing any task.

For example: Class geometry

{

float area;

virtual float cal-area();

}

What is a virtual destructor?

The virtual destructor is used to declare in the virtual base class. The virtual destructor is used to force the derived class to redefine the destructor. The base class body of the destructor is still called part of destruction any time a virtual function is used in a class a virtual destructor is immediately added even if it does nothing.

What is a template?

The template is a mechanism that makes it possible to use one function or class to handle many different data types. By using a template a single class or a function can operate on data of many types instead of having to create separate classes or functions for each data type. The functions are called function templates. Whereas the classes are called class templates.

Exit mobile version