Codes Academy

INHERITANCE IN C++

INHERITANCE IN C++

The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important feature of Object Oriented Programming.
Sub Class: The class that inherits properties from another class is called Sub class or Derived Class.
Super Class:The class whose properties are inherited by sub class is called Base Class or Super class.
The article is divided into following subtopics:
Why and when to use inheritance?
Consider a group of vehicles. You need to create classes for Bus, Car and Truck. The methods fuelAmount(), capacity(), applyBrakes() will be same for all of the three classes. If we create these classes avoiding inheritance then we have to write all of these functions in each of the three classes as shown in below figure:


class
You can clearly see that above process results in duplication of same code 3 times. This increases the chances of error and data redundancy. To avoid this type of situation, inheritance is used. If we create a class Vehicle and write these three functions in it and inherit the rest of the classes from the vehicle class, then we can simply avoid the duplication of data and increas re-usability. Look at the below diagram in which the three classes are inherited from vehicle class:

class
Using inheritance, we have to write the functions only one time instead of three times as we have inherited rest of the three classes from base class(Vehicle).

Implementing inheritance in C++: For creating a sub-class which is inherited from the base class we have to follow the below syntax.
Syntax:
class subclass_name : access_mode base_class_name
{   //body of subclass
};
Here, subclass_name is the name of the sub class, access_mode is the mode in which you want to inherit this sub class for example: public, private etc. and base_class_name is the name of the base class from which you want to inherit the sub class.
Note: private member of the base class will never get inherited in the sub class.
// C++ program to demonstrate implementation
// of Inheritance
  
#include <bits/stdc++.h>
using namespace std;

//Base class
class Parent
{
    public:
      int id_p;
};
  
// Sub class inheriting from Base Class(Parent)
class Child : public Parent
{
    public:
      int id_c;
};

//main function
int main()
   {
      
        Child obj1;
          
        // An object of class child has all data members
        // and member functions of class parent
        obj1.id_c = 7;
        obj1.id_p = 91;
        cout << "Child id is " <<  obj1.id_c << endl;
        cout << "Parent id is " <<  obj1.id_p << endl;
         
        return 0;
   }
Output:
Child id is 7
Parent id is 91
In the above program the ‘Child’ class is publicly inherited from the ‘Parent’ class so the public data members of the class ‘Parent’ will also be inherited by the class ‘Child’.
Thank you guys for reading my blogs.
Follow me on :

constructors and destructors

constructors
c++

Constructors are special class members which are called by the compiler every time an object of that class is instantiated. Constructors have the same name as the class and may be defined inside or outside the class definition.
There are 3 types of constructors:
§  Parametrized constructors
// C++ program to demonstrate constructors

#include <bits/stdc++.h>
using namespace std;
class Geeks
{
    public:
    int id;
     
    //Default Constructor
    Geeks()
    {
        cout << "Default Constructor called" << endl;
        id=-1;
    }
     
    //Parametrized Constructor
    Geeks(int x)
    {
        cout << "Parametrized Constructor called" << endl;
        id=x;
    }
};
int main() {
     
    // obj1 will call Default Constructor
    Geeks obj1;
    cout << "Geek id is: " <<obj1.id << endl;
     
    // obj1 will call Parametrized Constructor
    Geeks obj2(21);
    cout << "Geek id is: " <<obj2.id << endl;
    return 0;
}
Run on IDE
Output:
Default Constructor called
Geek id is: -1
Parametrized Constructor called
Geek id is: 21
A Copy Constructor creates a new object, which is exact copy of the existing copy. The compiler provides a default Copy Constructor to all the classes.
Syntax:
class-name (class-name &){}
Destructor is another special member function that is called by the compiler when the scope of the object ends.
// C++ program to explain destructors

#include <bits/stdc++.h>
using namespace std;
class Geeks
{
    public:
    int id;
     
    //Definition for Destructor
    ~Geeks()
    {
        cout << "Destructor called for id: " << id <<endl;
    }
};

int main()
  {
     
    Geeks obj1;
    obj1.id=7;
    int i = 0;
    while ( i < 5 )
    {
        Geeks obj2;
        obj2.id=i;
        i++;
         
    } // Scope for obj2 ends here
    scope
    return 0;
  } // Scope for obj1 ends here
Run on IDE
Output:
Destructor called for id: 0
Destructor called for id: 1
Destructor called for id: 2
Destructor called for id: 3
Destructor called for id: 4
Destructor called for id: 7


TYPES OF MEMBER FUNCTIONS

TYPES OF MEMBER FUNCTIONS
We already know what member functions are and what they do. Now lets study some special member functins present in the class. Following are different types of Member functions,
1.  Simple functions
2.  Static functions
3.  Const functions
4.  Inline functions
5.  Friend functions

Simple Member functions

These are the basic member function, which dont have any special keyword like static etc as prefix. All the general member functions, which are of below given form, are termed as simple and basic member functions.
return_type functionName(parameter_list)
{
function body;
}

Static Member functions

Static is something that holds its position. Static is a keyword which can be used with data members as well as the member functions. We will discuss this in details later. As of now we will discuss its usage with member functions only.
A function is made static by using static keyword with function name. These functions work for the class as whole rather than for a particular object of a class.
It can be called using the object and the direct member access . operator. But, its more typical to call a static member function by itself, using class name and scope resolution :: operator.
Example :
class X
{  public:
static void f(){};
};
int main()
{
 X::f();   // calling member function directly with class name
}
These functions cannot access ordinary data members and member functions, but only static data members and static member functions.
It doesn't have any "this" keyword which is the reason it cannot access ordinary members. We will study about "this" keyword later.

Const Member functions

We will study Const keyword in detail later, but as an introduction, Const keyword makes variables constant, that means once defined, there values can't be changed.
When used with member function, such member functions can never modify the object or its related data members.
//Basic Syntax of const Member Function
void fun() const {}

Inline functions

All the member functions defined inside the class definition are by default declared as Inline. We will study Inline Functions in details in the next topic.

Friend functions

Friend functions are actually not class member function. Friend functions are made to give private access to non-class functions. You can declare a global function as friend, or a member function of other class as friend.
Example :
class WithFriend
{
 int i;
 public:
 friend void fun(); // Global function as friend
};

void fun()
{
 WithFriend wf;
 wf.i=10;  // Access to private data member
 cout << wf.i;
}
int main()
{
fun(); //Can be called directly
}
Hence, friend functions can access private data members by creating object of the class. Similarly we can also make function of other class as friend, or we can also make an entire class as friend class.
class Other
{
 void fun();
};
class WithFriend
{
 private:
 int i;
 public:
 void getdata();  // Member function of class WithFriend
 friend void Other::fun();   // making function of class Other as friend here
 friend class Other;  // making the complete class as friend
};
When we make a class as friend, all its member functions automatically become friend functions.
Friend Functions is a reason, why C++ is not called as a pure Object Oriented language. Because it violates the concept of Encapsulation.
Thank you friends for reading my blog.


MEMBER FUNCTIONS IN CLASSES

MEMBER FUNCTIONS IN CLASSES

C++
Member functions are the functions, which have their declaration inside the class definition and works on the data members of the class. The definition of member functions can be inside or outside the definition of class.
Member functions are the functions, which have their declaration inside the class definition and works on the data members of the class. The definition of member functions can be inside or outside the definition of class.
If the member function is defined inside the class definition it can be defined directly, but if its defined outside the class, then we have to use the scope resolution :: operator along with class name alng with function name.
Example :
class Cube
{ public:
int side;
 int getVolume();     // Declaring function getVolume with no argument and return type int.
};
If we define the function inside class then we don't not need to declare it first, we can directly define the function.
class  Cube{
public:
 int side;
 int getVolume()
 {  return side*side*side;       //returns volume of cube
 }
};
But if we plan to define the member function outside the class definition then we must declare the function inside class definition and then define it outside.
class Cube
{ public:
 int side;
 int getVolume();
}
int Cube :: getVolume()     // defined outside class definition
{
 return side*side*side;
}
The maine function for both the function definition will be same. Inside main() we will create object of class, and will call the member function using dot (.) operator.
int main()
{
 Cube C1;
 C1.side=4;   // setting side value
 cout<< "Volume of cube C1 ="<< C1.getVolume();
}
Similarly we can define the getter and setter functions to access private data members, inside or outside the class definition.
Thank  you guys for reading my blogs .
Thank you


Follow me on social  sites :
Ø      Follow me on google  plus.

ACCESSING DATA MEMBERS OF CLASS

ACCESSING DATA MEMBERS OF CLASS
The data members and member functions of class can be accessed using the dot(‘.’) operator with the object.
For example  if the name of object is obj and you want to access the member function with the name printName() then you will have to write obj.printName() .
If, the data member is defined as private or protected, then we cannot access the data variables directly. Then we will have to create special public member functions to access, use or initialize the private and protected data members. These member functions are also called Accessors and Mutator methods or getter and setter functions.

Accessing Public Data Members

Following is an example to show you how to initialize and use the public data members using the dot (.) operator and the respective object of class.
class Student
{  public:
 int rollno;
 string name;
};

int main()
{ Student A;
 Student B;
 A.rollno=1;
 A.name="Adam";
B.rollno=2;
 B.name="Bella";
cout <<"Name and Roll no of A is :"<< A.name << A.rollno;
 cout <<"Name and Roll no of B is :"<< B.name << B.rollno;
}

Accessing Private Data Members

To access, use and initialize the private data member you need to create getter and setter functions, to get and set the value of the data member.
The setter function will set the value passed as argument to the private data member, and the getter function will return the value of the private data member to be used. Both getter and setter function must be defined public.
Example :
class Student
{   private:    // private data member
 int rollno;
public:     // public accessor and mutator functions
 int getRollno()
 {
  return rollno;
 }
void setRollno(int i)
 {
  rollno=i;
 }
};
int main()
{ Student A;
 A.rollono=1;  //Compile time error
 cout<< A.rollno; //Compile time error
A.setRollno(1);  //Rollno initialized to 1
 cout<< A.getRollno(); //Output will be 1
}
So this is how we access and use the private data members of any class using the getter and setter methods. We will discuss this in more details later.

Accessing Protected Data Members

Protected data members, can be accessed directly using dot (.) operator inside the subclass of the current class, for non-subclass we will have to follow the steps same as to access private data member.
Example to show  all  the  data  members  in a class
// C++ program to demonstrate
// accessing of data members

#include <bits/stdc++.h>
using namespace std;
class Geeks
{
    // Access specifier
    public:

    // Data Members
    string geekname;

    // Member Functions()
    void printname()
    {
       cout << "Geekname is: " << geekname;
    }
};

int main() {
  // Declare an object of class geeks
    Geeks obj1;

    // accessing data member
    obj1.geekname = "Abhi";
 // accessing member function
    obj1.printname();
    return 0;
}
Thank  you giuys for reading my blogs if  my blogs seems interesting so please like them and gave advise me what can I correct my blogs.
Thank you


Follow me on social  sites :
Ø      Follow me on google  plus.