GoConqr hat ein neues Design! Sag uns, wie es dir gefällt.

Notizen

OOPs

Abhay Chandna Englisch 10 Seiten

Inhalt der Notizen (10)

Seite 1

References

https://www.javatpoint.com/cpp-inheritance

https://www.interviewbit.com/oops-interview-questions/

Seite 2

Friend Functions - Functions which can be used outside class

#include

using namespace std;

class Box

{

private:

int length;

public:

Box(): length(0) { }

friend int printLength(Box); //friend function

};

int printLength(Box b)

{

b.length += 10;

return b.length;

}

int main()

{

Box b;

cout<<"Length of box: "<< printLength(b)<

return 0;

}

Seite 3

Inheritance

class derived_class_name :: visibility-mode base_class_name

{

// body of the derived class.

}

Default visibility mode is public

Visibility of Inherited Members ( https://www.codespeedy.com/visibility-modes-in-cpp/ )

if derived is private - no variables got from parent,

'' prtorected - all variables inhertied with prev visibilty values, public convered to protected

"" public - all variables inhertied with prev visibilty values

Base class visibilityDerived class visibility

Public Private Protected

Private Not Inherited Not Inherited Not Inherited

Protected Protected Private Protected

Public Public Private Protected

Multipel inheritance

class D : visibility B-1, visibility B-2, ?

{

// Body of the class;

}

Ambiguity resolution if A is derived from B and C both and noth partents have same function name

A.show() doesnt know which function to call, (from B or from C)

Prenvent that using a_obj.B::show() or a_obj.C::show()

ch.A:: show(); // class_name and scope resolution operator with member function

ch.B::show();

Seite 4

Polymorphism

Static vs dynamic

Differences b/w compile time and run time polymorphism.

Compile time polymorphismRun time polymorphism

The function to be invoked is known at the compile time.The function to be invoked is known at the run time.

It is also known as overloading, early binding and static binding.It is also known as overriding, Dynamic binding and late binding.

Overloading is a compile time polymorphism where more than one method is having the same name but with the different number of parameters or the type of the parameters.Overriding is a run time polymorphism where more than one method is having the same name, number of parameters and the type of the parameters.

It is achieved by function overloading and operator overloading.It is achieved by virtual functions and pointers.

It provides fast execution as it is known at the compile time.It provides slow execution as it is known at the run time.

It is less flexible as mainly all the things execute at the compile time.It is more flexible as all the things execute at the run time.

#include

using namespace std;

class Animal { // base class declaration.

public:

string color = "Black";

};

class Dog: public Animal // inheriting Animal class.

{

public:

string color = "Grey";

};

int main(void) {

Animal d= Dog();

cout<

}

OP - black

Function Overloading and Ambiguity

When the compiler is unable to decide which function is to be invoked among the overloaded function, this situation is known as function overloading.

When the compiler shows the ambiguity error, the compiler does not run the program.

Causes of Function Overloading:

Type Conversion.

Function with default arguments.

Function with pass by reference.

Seite 5

Virtual funtion

A C++ virtual function is a member function in the base class that you redefine in a derived class. It is declared using the virtual keyword

There is a necessity to use the single pointer to refer to all the objects of the different classes. So, we create the pointer to the base class that refers to all the derived objects. But, when base class pointer contains the address of the derived class object, always executes the base class function. This issue can only be resolved by using the 'virtual' function.

Late binding or Dynamic linkage

In late binding function call is resolved during runtime. Therefore compiler determines the type of object at runtime, and then binds the function call.

Rules of Virtual Function

Virtual functions must be members of some class.

Virtual functions cannot be static members.

They are accessed through object pointers.

They can be a friend of another class.

A virtual function must be defined in the base class, even though it is not used.

The prototypes of a virtual function of the base class and all the derived classes must be identical. If the two functions with the same name but different prototypes, C++ will consider them as the overloaded functions.

We cannot have a virtual constructor, but we can have a virtual destructor

Consider the situation when we don't use the virtual keyword.

A *a = B();

A.show();

For non virtual A will run and for virtual B will run

Deleting a derived class object using a pointer of base class type that has a non-virtual destructor results in undefined behavior. To correct this situation, the base class should be defined with a virtual destructor - https://www.geeksforgeeks.org/virtual-destructor/

Instantiated with A so all func of A will be called. If func is virtual, A will be called. If func is virtual and overridden in derived B , then B will be called.

Vtable (advanced tno need to do) https://www.geeksforgeeks.org/virtual-function-cpp/

Seite 6

Pure Virtual Functions

A virtual function is not used for performing any task. It only serves as a placeholder.

When the function has no definition, such function is known as "do-nothing" function.

The "do-nothing" function is known as a pure virtual function. A pure virtual function is a function declared in the base class that has no definition relative to the base class.

A class containing the pure virtual function cannot be used to declare the objects of its own, such classes are known as abstract base classes.

The main objective of the base class is to provide the traits to the derived classes and to create the base pointer used for achieving the runtime polymorphism.

virtual void display() = 0;

Class having pure is abstrac class.

Virtual functions need to redefined in derived class

Object of abstract class cannot be created

Seite 7

Abstraction and Intercaces

Abstract classes are the way to achieve abstraction in C++. Abstraction in C++ is the process to hide the internal details and showing functionality only. Abstraction can be achieved by two ways:

Abstract class

Interface

Abstract class and interface both can have abstract methods which are necessary for abstraction.

Fully abstracted class is interface

Advantages Of Abstraction:

Implementation details of the class are protected from the inadvertent user level errors.

A programmer does not need to write the low level code.

Data Abstraction avoids the code duplication, i.e., programmer does not have to undergo the same tasks every time to perform the similar operation.

The main aim of the data abstraction is to reuse the code and the proper partitioning of the code across the classes.

Internal implementation can be changed without affecting the user level code.

Seite 8

Static Members

Variables and functions are global.

Seite 9

Constructors

class A:public B

{

private:

int x;

public:

A(int x){

this->x = x;

}

};

or

#include

using namespace std;

class Complex

{

private:

double real;

double imag;

public:

// Default constructor

Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

// A method to compare two Complex numbers

bool operator == (Complex rhs) {

return (real == rhs.real && imag == rhs.imag)? true : false;

}

};

int main()

{

// a Complex object

Complex com1(3.0, 0.0);

if (com1 == 3.0)

cout << "Same";

else

cout << "Not Same";

return 0;

}

Seite 10

Multiple Inheritance (Diamond Problem)

https://www.geeksforgeeks.org/multiple-inheritance-in-c/

A->B, A->C, D->B,C (A is grandparent, B and C are parents)

If we construct D:B,C , constructor calling order - A,B,A,C,D(Destructors called in reverse order)

To prevent 2 constructions of A, make B and C virtual classes

class A{}

class B: virtual public A{}

class C: virtual public A{}

class D: public B, public C {}

this prevents 2 constructions

Informationen

Beschreibung
Notiz am OOPs, erstellt von Abhay Chandna am 23/08/2021.
Erstellt von
Abhay Chandna
Aufrufe
0
Bewertung

Feedback

Worum geht es?

Wie gefällt dir das neue Design?

Dein Eindruck