Difference between Base class and Derived class in C++ - learnit

Home Top Ad

Post Top Ad

Wednesday, January 12, 2022

Difference between Base class and Derived class in C++

Difference between Base class and Derived class in C++

Base Class: In the Object-Oriented Programming language, a base class is a class from which other classes are derived. The class that inherits the base class has all of the base class's members as well as some additional properties. The members and member functions of the base class are passed down to the derived class's Object. A base class is also referred to as a parent class or a superclass.


Derived Class: A class that is derived from another class. The derived class inherits all of the base class's members and member functions. The derived class may have more functionality than the Base class and may have easier access to the Base class. A Derived class is also referred to as a child class or a subclass.


Syntax for creating Derive Class:


class BaseClass{
// members....
// member function
}


class DerivedClass : public BaseClass{
// members....
// member function
}

No Base Class Derived Class
1 A class from which properties are inherited. A class from which is inherited from the base class.
2 It is also known as parent class or superclass. It is also known as child class subclass.
3 It cannot inherit properties and methods of Derived Class. It can inherit properties and methods of Base Class.
4 Syntax: Class base_classname{ … }. Syntax: Class derived_classname : access_mode base_class_name { … }.

Example
#include<stdio.h>
#include<conio.h>
#include <iostream>
using namespace std;
class base{
public:
int a;
};
class Derive:public base{
public:
int b;
};
int main(){
Derive obj;
obj.a=10;
cout<<"Number A="<<"\t"<<obj.a<<endl;
obj.b=20;
cout<<"Number B="<<"\t"<<obj.b<<endl;
}

Please Watching My Video is Below

No comments:

Post a Comment

Post Top Ad