Posts

Showing posts from June, 2020

Problem 1 Inheritance OOP

Image
Problem:                   You have to make three classes: Person class Student class Employee class Criteria:                 Person class is the parent class or base class. Student class is derived or child of Person class and Employee class is also derived or child of Person class.          Member variables and methods/functions: Person class:                   Person class must have 3 private member variables  Name CNIC Address                And two public member functions: get set   To get and set values of member variables. Student class:                Student class must have 4 private member variables: RollNo Sub1 Sub2 Sub3               Three public member functions/methods: get1 set1  calcG...

Inheritance in C++ part 1

Image
Inheritance: A programming technique that is used to reuse an existing class to create a new class is called inheritance . The new class inherits all the behaviour of the original class. The existing class that is raised to create a new class is called super class, base class or parent class. The new class that inherits the properties and functions of the existing class is known as subclass, derived class or child class. The basic principle of inheritance is that each child class shares common properties with the class from which it is derived. The child class inherits all properties of the parent class and can add its own capabilities. Example: Suppose we have a class named Vehicle. The derived classes of this class may share similar properties such as wheels and motor etc. Additionally, derived classes may have their own particular characteristics. For example, a derived class bus may have seats for people but, another derived class truck may have space to carry goods. Advantages of ...

Abstraction and Encapsulation

Image
Encapsulation: Encapsulation is a process of combining data members and functions in a single unit called class. This is to prevent the access to the data directly. To get access to the data member functions of the class are used. It helps in data hiding. It is one of the popular features of object oriented programming.   How to achieve Encapsulation in a class: For this purpose you have to make all the data members private. Then create public getter and setter methods in such a way that the way function set the value of the data member and the hat function get the value of the data member. Abstraction:    Abstraction is similar to the concept of a black box. Input goes in, black box does something and output comes out. It doesn't matter what happens in the black box, all you have to know is that it works.    It means displaying only essential information and hiding the background details or implementation. Real world example of abstraction:     ...

Getter and Setter in C++

Image
Getter and Setter:            Getters and setters  are used to efficiently protect your data. This is a technique used greatly when creating classes.  For each instance variable, a  getter  method returns its value while a  setter  method sets or updates its value. The  getter  method returns the value of the attribute. The  setter  method takes a parameter and assigns it to the attribute. The  getters and setters  are usually public and the variables are made. Why use getter and setter: Getters and setters  makes your code more manageable and more future proof.  If you ever want to change the class, and you want to drop the field, you can emulate it and your application code is not broken.  You will be helpful in large projects. Consuming class would be more homogeneous. ☝ Getters and Setters are a common technique used in object oriented programming. It is good practice to use thes...

Constructor in C++

Constructor:                 Constructor is a special type of member function that is automatically called when an object of the class is created to whom the constructor belongs. Properties of constructor: A constructor initializes an object as soon as it is created. Constructor name is same as class name and it doesn't have a return type.       class A         {        public:               A ( )    // constructor of class A                 {                   // object initialization                  }         }; Constructors can be defined either inside the class definition or outside class definition using class name and scope resolution ::  operator. Uses of constructor: Constructor ...