You are on page 1of 25

OOPS IMPORTANT

Question 1. Difference between OOP


and POP ?

• POP • OOP
• It is a programming pattern which is based upon the concept of procedure calls • It is a programming pattern which is based upon the concept of objects,
POP. which contains data member and member functions is calls OOP.
• It stands for Object Oriented Programming,
• It stands for Procedure Oriented Programming.
• Emphasis on objects and classes.
• Emphasis on functions.
• Divides the program into multiple objects.
• Divides the program into multiple functions.
• Modification is easier as objects are independent.
• Modification is difficult as they can affect the entire program.
• Objects communicate with each other by passing message.
• Function communicate with each other by passing parameters.
• Function share global variable. • Each object control its own data.

• There is no data hiding mechanism. • It is possible to hide data.


• Do not have access specifier. • Has access specifier.
• Top – Down approach. • Bottom-Up approach.
• Supported by :- C , Pascal , Fortran, etc. • Supported by :- C++ , JAVA, Python etc.
Question 2. Difference between structure and class ?

Structure Classes
• Structure is a collection of different types of data type. • Class is a collection of common objects that share common properties and
relation.
• Does not support inheritance
• Class support inheritance.
• Structure member are public by default. So anyone can access them.
• Class member are private by default. So only the object can access them.
• Structure are value type i.e. they are stored in stack.
• Classes are reference type i.e. they are stored in heap.
• Structure should be used when the program is small.
• Classes are better choice for complex programs.
• Structure element can’t be declared as protected.
• Class element can be declared as protected.
• Structure does not require constructor.
• Class does require constructor.
• They are not secure.
• Classes are secure.
• Structure:-
• Class :-
• struct MyStructure { // Structure declaration
• class MyClass { // The class
• int myNum; // Member (int variable)
• public: // Access specifier
• char myLetter; // Member (char variable)
• int myNum; // Attribute (int variable)
• }; // End the structure with a semicolon
• string myString; // Attribute (string variable)
• };
Question 3. Characteristics of OOPS.

• Following are the basic characteristics of oops :-


• Classes are user-defined data types that act as the blueprint for individual objects, attributes and methods.
• Objects are instances of a class created with specifically defined data. Objects can correspond to real-world objects or an abstract entity.
When class is defined initially, the description is the only object that is defined.
• Methods are functions that are defined inside a class that describe the behaviors of an object. Each method contained in class definitions
starts with a reference to an instance object. Additionally, the subroutines contained in an object are called instance methods. Programmers
use methods for reusability or keeping functionality encapsulated inside one object at a time.
• Attributes are defined in the class template and represent the state of an object. Objects will have data stored in the attributes field. Class
attributes belong to the class itself
• Encapsulation. This principle states that all important information is contained inside an object and only select information is exposed. The
implementation and state of each object are privately held inside a defined class. Other objects do not have access to this class or the
authority to make changes. They are only able to call a list of public functions or methods. This characteristic of data hiding provides greater
program security and avoids unintended data corruption.
• Abstraction. Objects only reveal internal mechanisms that are relevant for the use of other objects, hiding any unnecessary implementation
code. The derived class can have its functionality extended. This concept can help developers more easily make additional changes or
additions over time.
• Inheritance. Classes can reuse code from other classes. Relationships and subclasses between objects can be assigned, enabling developers
to reuse common logic while still maintaining a unique hierarchy. This property of OOP forces a more thorough data analysis, reduces
development time and ensures a higher level of accuracy.
• Polymorphism. Objects are designed to share behaviors and they can take on more than one form. The program will determine which
meaning or usage is necessary for each execution of that object from a parent class, reducing the need to duplicate code. A child class is
then created, which extends the functionality of the parent class. Polymorphism allows different types of objects to pass through the same
interface.
Call by value and Call by reference

• Call by value:- In call by value, original value is not modified . In call by value, value being passed to the function is locally
stored by the function parameter in stack memory location. If you change the value of function parameter, it is changed for
the current function only. It will not change the value of variable inside the caller method such as main().
• #include <iostream>
• using namespace std;
• void change(int data);
• int main()
• {
• int data = 3;
• change(data);
• cout << "Value of the data is: " << data<< endl;
• return 0;
• }
• void change(int data)
• {
• data = 5;
• }
• OUTPUT :- Value of the data is: 3.
Call by reference :-
• In call by reference, original value is modified because we pass reference (address) . Here, address of the value is passed in the function, so actual
and formal arguments share the same address space. Hence, value changed inside the function, is reflected inside as well as outside the function.
• Example :-
• #include<iostream>
• using namespace std;
• void swap(int *x, int *y)
• {
• int swap;
• swap=*x;
• *x=*y;
• *y=swap;
• }
• int main()
• {
• int x=500, y=100;
• swap(&x, &y); // passing value to function
• cout<<"Value of x is: "<<x<<endl;
• cout<<"Value of y is: "<<y<<endl;
• return 0;
• }
• OUTPUT:- Value of x is: 100 Value of y is: 500
Classes and Objects
The main purpose of C++ programming is to add object orientation to the C programming language and classes are the central
feature of C++ that supports object-oriented programming and are often called user-defined types.A class is used to specify the
form of an object and it combines data representation and methods for manipulating that data into one neat package. The data
and functions within a class are called members of the class.

Class:- When you define a class, you define a blueprint for a data type. This doesn't actually define any data, but it does define what
the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.

A class definition starts with the keyword class followed by the class name; and the class body, enclosed by a pair of curly braces. A
class definition must be followed either by a semicolon or a list of declarations. For example, we defined the Box data type using the
keyword class as follows −
class Box {
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
The keyword public determines the access attributes of the members of the class that follows it. A public member can be accessed from
outside the class anywhere within the scope of the class object. You can also specify the members of a class as private or protected which
we will discuss in a sub-section.
Object:-

• A class provides the blueprints for objects, so basically an object is created from a class. We declare
objects of a class with exactly the same sort of declaration that we declare variables of basic types.
Following statements declare two objects of class Box −

• Box Box1; // Declare Box1 of type Box


• Box Box2; // Declare Box2 of type Box

• Both of the objects Box1 and Box2 will have their own copy of data members.
• Example-:
• #include <iostream>
• using namespace std;
• class Box {
• public:
• double length; // Length of a box
• double breadth; // Breadth of a box
• double height; // Height of a box
• };
• int main() {
• Box Box1; // Declare Box1 of type Box
• Box Box2; // Declare Box2 of type Box
• double volume = 0.0; // Store the volume of a box here
• // box 1 specification
• Box1.height = 5.0;
• Box1.length = 6.0;
• Box1.breadth = 7.0;
• // box 2 specification
• Box2.height = 10.0;
• Box2.length = 12.0;
• Box2.breadth = 13.0;
• // volume of box 1
• volume = Box1.height * Box1.length * Box1.breadth;
• cout << "Volume of Box1 : " << volume <<endl;
• // volume of box 2
• volume = Box2.height * Box2.length * Box2.breadth;
• cout << "Volume of Box2 : " << volume <<endl;
• return 0;
• }
Friend function
• C++ Friend function :-
• If a function is defined as a friend function in C++, then the protected and private data of a class can be accessed using the function.By using
the keyword friend compiler knows the given function is a friend function . For accessing the data, the declaration of a friend function
should be done inside the body of a class starting with the keyword friend.
• class class_name
• {
• friend data_type function_name(argument/s); // syntax of friend function.
• };

• Characteristics of a Friend function:-

• The function is not in the scope of the class to which it has been declared as a friend.
• It cannot be called using the object as it is not in the scope of that class.
• It can be invoked like a normal function without using the object.
• It cannot access the member names directly and has to use an object name and dot membership operator with the member name.
• It can be declared either in the private or the public part.
1. #include <iostream>  

2. using namespace std;  

3. class B;          // forward declarartion.  

4. class A  

5. {  

6.     int x;  

7.     public:  

8.     void setdata(int i)  

9.     {  

10.         x=i;  

11.     }  

12.     friend void min(A,B);         // friend function.  

13. };  

14. class B  

15. {  

16.     int y;  

17.     public:  

18.     void setdata(int i)  

19.     {  

20.         y=i;  

21.     }  

22.     friend void min(A,B);                    // friend function  

23. };  

24. void min(A a,B b)  

25. {  

26.     if(a.x<=b.y)  

27.     std::cout << a.x << std::endl;  

28.     else  

29.     std::cout << b.y << std::endl;  

30. }  
1.   int main()  

2. {  

3.    A a;  

4.    B b;  

5.    a.setdata(10);  

6.    b.setdata(20);  

7.    min(a,b);  

8.     return 0;  

9.  }  

10. OOUTPU:- 10
What is constructor and destructor

• Constructor:- In C++, constructor is a special method which is invoked automatically at the time of object creation. It is
used to initialize the data members of new object generally. The constructor in C++ has the same name as class or
structure.
• There can be three types of constructors in C++.
• Default constructor
• Parameterized constructor
• Copy constructor
• 1.Default constructor:- A constructor with no parameters is known as a default constructor.
• // C++ program to demonstrate the use of default constructor
• #include <iostream>
• using namespace std;
• // declare a class
• class Wall {
• private:
• double length;
• public:
• // default constructor to initialize variable
• Wall() {
• length = 5.5;
• cout << "Creating a wall." << endl;
• cout << "Length = " << length << endl;
• }
• };
• int main() {
• Wall wall1;
• return 0;
• }
• OUTPUT:-
• Creating a Wall
• Length = 5.5
• Parameterized Constructor:- In C++, a constructor with parameters is known as a parameterized constructor. This is preferred method to initialize member data.

• #include <iostream>

• using namespace std;

• // declare a class

• class Wall {

• private:

• double length;

• double height;

• public:

• // parameterized constructor to initialize variables

• Wall(double len, double hgt) {

• length = len;

• height = hgt;

• }

• double calculateArea() {

• return length * height;

• }

• };

• int main() {

• // create object and initialize data members

• Wall wall1(10.5, 8.6);

• Wall wall2(8.5, 6.3);

• cout << "Area of Wall 1: " << wall1.calculateArea() << endl;

• cout << "Area of Wall 2: " << wall2.calculateArea();

• return 0;

• }
• C++ Copy Constructor:- The copy constructor in C++ is used to copy data of one object to another.
• #include <iostream>
• using namespace std;
• class Wall {
• private:
• double length;
• double height;
• public:
• Wall(double len, double hgt) {
• length = len;
• height = hgt;
• }
• Wall(Wall &obj) {
• length = obj.length;
• height = obj.height;
• }
• double calculateArea() {
• return length * height;
• }
• };
• int main() {
• Wall wall1(10.5, 8.6);
• Wall wall2 = wall1;
• cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
• cout << "Area of Wall 2: " << wall2.calculateArea();
• return 0;
• }
• Area of Wall 1: 90.3
• Area of Wall 2: 90.3
DESTRUCTOR
• A destructor works opposite to constructor; it destructs the objects of classes. It can be defined only once in a class. Like constructors, it is invoked automatically.A
destructor is defined like constructor. It must have same name as class. But it is prefixed with a tilde sign (~).
1. #include <iostream>  

2. using namespace std;  

3. class Employee  

4.  {  

5.    public:  

6.         Employee()    

7.         {    

8.             cout<<"Constructor Invoked"<<endl;    

9.         }    

10.         ~Employee()    

11.         {    

12.             cout<<"Destructor Invoked"<<endl;    

13.         }  

14. };  

15. int main(void)   

16. {  

17.     Employee e1; //creating an object of Employee   

18.     Employee e2; //creating an object of Employee  

19.     return 0;  

20. }  

• Constructor Invoked
• Constructor Invoked
• Destructor Invoked
• Destructor Invoked
Inheritance and its type

• inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such way, you can
reuse, extend or modify the attributes and behaviors which are defined in other class. In C++, the class which inherits the members of
another class is called derived class and the class whose members are inherited is called base class. The derived class is the specialized class
for the base class.
• Benefits of Inheritance
• Inheritance helps in code reuse. The child class may use the code defined in the parent class without re-writing it.
• Inheritance can save time and effort as the main code need not be written again.
• Inheritance provides a clear model structure which is easy to understand.
• An inheritance leads to less development and maintenance costs.
• With inheritance, we will be able to override the methods of the base class so that the meaningful implementation of the base class method
can be designed in the derived class. An inheritance leads to less development and maintenance costs.
• In inheritance base class can decide to keep some data private so that it cannot be altered by the derived class.
• Types of inheritance:-
• 5 types of inheritance-
• Single Inheritance.
• Multiple Inheritance.
• Multilevel Inheritance.
• Hierarchical Inheritance.
• Hybrid Inheritance.
1. #include <iostream>  
2. using namespace std;  
3.  class Account {  
SINGLE INHERITANCE
4.    public:  
Single inheritance is defined as
5.    float salary = 60000;   
the inheritance in which a
derived class is inherited from 6.  };  
the only one base class. 7.    class Programmer: public Account {  
When one class inherits another 8.    public:  
class, it is known as single level
inheritance. 9.    float bonus = 5000;    
10.   };       
11.int main(void) {  
12.     Programmer p1;  
13.     cout<<"Salary: "<<p1.salary<<endl;    
14.     cout<<"Bonus: "<<p1.bonus<<endl;    
15.    return 0;  
16.}  
• Salary: 60000
• Bonus: 5000
1. #include <iostream>  

2. using namespace std;  

3. class A  

4. {  

5.     protected:  

6.      int a;  
Multiple Inheritance 7.     public:  

8.     void get_a(int n)  

Multiple inheritance is the process of 9.     {  


deriving a new class that inherits the 10.         a = n;  
attributes from two or more classes. 11.     }  

class D : visibility B-1, visibility B-2, ? 12. };  

13.   
{
14. class B  
// Body of the class; 15. {  

} 16.     protected:  

17.     int b;  

18.     public:  

19.     void get_b(int n)  

20.     {  

21.         b = n;  

22.     }  

23. };
1.   class C : public A,public B  

2. {  

3.    public:  

4.     void display()  

5.     {  

6.         std::cout << "The value of a is : " <<a<< std::endl;  

7.         std::cout << "The value of b is : " <<b<< std::endl;  

8.         cout<<"Addition of a and b is : "<<a+b;  

9.     }  

10. };  

11. int main()  

12. {  

13.    C c;  

14.    c.get_a(10);  

15.    c.get_b(20);  

16.    c.display();  

17.     return 0;  

18. }  

• The value of a is : 10

• The value of b is : 20

• Addition of a and b is : 30
• #include <iostream>

C++ Multilevel Inheritance • using namespace std;

In C++ programming, not only you can


derive a class from the base class but • class A {
you can also derive a class from the • public:
derived class. This form of inheritance is
known as multilevel inheritance. • void display() {
• cout<<"Base class content.";
• }
• };

• class B : public A {};

• class C : public B {};

• int main() {
• C obj;
• obj.display();
• return 0;
• }
• Base class content.
• #include <iostream>
• using namespace std;

• // base class
• class Animal {
• public:
• void info() {
• cout << "I am an animal." << endl;
C++ Hierarchical Inheritance • }
• };
If more than one class is inherited from the
base class, it's known as
hierarchical inheritance. In hierarchical • // derived class 1
inheritance, all features that are common in • class Dog : public Animal {
child classes are included in the base class. • public:
• void bark() {
• cout << "I am a Dog. Woof woof." << endl;
• }
• };

• // derived class 2
• class Cat : public Animal {
• public:
• void meow() {
• cout << "I am a Cat. Meow." << endl;
• }
• };
• int main() {
• // Create object of Dog class
• Dog dog1;
• cout << "Dog Class:" << endl;
• dog1.info(); // Parent Class function
• dog1.bark();
• Cat cat1;
• cout << "\nCat Class:" << endl;
• cat1.info(); // Parent Class function
• cat1.meow();
• return 0;
• }
• Dog Class:
• I am an animal.
• I am a Dog. Woof woof.

• Cat Class:
• I am an animal.
• I am a Cat. Meow.
Virtual base class

• • Virtual Base Class: Virtual Class is defined by writing a keyword “virtual” in the derived classes, allowing
only one copy of data to be copied to Class B and Class C (referring to the below example). It prevents
multiple instances of a class appearing as a parent class in the inheritance hierarchy when multiple
inheritances are used.
• class B : virtual public A
• {
• };
• Syntax 2:
• class C : public virtual A
• {
• };
#include <iostream>
using namespace std;
  
class A {
public:
    void show()
    {
        cout << "Hello from A \n";
    }
};
  
class B : public virtual A {
};
  
class C : public virtual A {
};
  
class D : public B, public C {
};
  
int main()
{
    D object;
    object.show();
}

Hello from A

You might also like