×

C++ Tutorial

C++ Data types

C++ Operators & Keywords

C++ Conditional Statements

C++ Functions

C++ 'this' Pointer, References

C++ Class & Objects

C++ Constructors & Destructors

C++ Operator overloading

C++ 11 (Advance C++)

C++ Preparation

C++ Header Files & Functionsr

Data Structure with C++

C++ - Miscellaneous

C++ Programs

Concepts of OOPS in C++ programming

Learn: What are the concepts of Object Oriented Programming Systems (OOPS) in C++ programming language? Brief description of Class, Object, Inheritance, Data Encapsulation, Data Abstraction and Polymorphism.

OOPs in C++ programming

OOPS stands for "Object Oriented Programming System" in C++ programming. OOPs programming approach which follows concept of object oriented programming like class, object, data abstraction & encapsulation, inheritance, polymorphism etc, is known as Object oriented programming. In short, we call it OOP’s (object oriented programming).

Here we are discussing abbot the concepts of OOPS (or main features of OOPS).

There are following concepts of OOPS:

  1. Class
  2. Object
  3. Inheritance
  4. Data Encapsulation
  5. Data Abstractions
  6. Polymorphism

Class

Class is the template of an object. That logically encapsulates data members and member functions into a single unit. Classes are data type based on which objects are created.

Class is a user define data type; we call it user define because a user can define the data and functions as per their requirements. To create a class, we first write class (class is a keyword) and then name of the class (mentioned below in example). Basically class divides in two main sections - data member and member function. Class is a unit which combines data member and member function together in a single unit.

Syntax

class class_name
{
    //data members section
    private:
        ...;                             
        ...;
   
    // member functions section
    public:
        ...;
        ...;
};

Object

Object is a basic unit of OOPS. It has unique name. An object represents a particular instance of a class. We can create more than one objects of a class. The size of class is size of total number of data members of class.

An object is a real world entity which has its name and own properties. For example- apple is an object and its properties are name, color, size and taste etc, like:

  • Name - apple
  • Color - red
  • Size - medium in size
  • Taste - sweet

Example of Class and Objects

#include <iostream>
using namespace std;

class student {
 private:
  // sname and srollno are data members
  char sname[30];
  int srollno;

 public:
  void getinput() {
    // get name from the Keyboard
    cout << "Input Students Name: ";
    cin >> sname;

    // get rollno from the Keyboard
    cout << "Input Students Roll Number: ";
    cin >> srollno;
  }
  void getoutput() {
    // shows name on monitor
    cout << "Students Name:" << sname << endl;
    // shows rollno on monitor
    cout << "Student Roll Number:" << srollno << endl;
  }
};

// main code
int main() {
  // s is an object of student class
  student s;

  // the getinput method is being called
  s.getinput();

  // the getoutput method is being called
  s.getoutput();

  return 0;
}

Output

Input Students Name: Alvin
Input Students Roll Number: 102
Students Name:Alvin
Student Roll Number:102

Inheritance

Inheritance is the process of creating new class from existing class or base class. By using the concept of Inheritance, we can use implemented (existing) features of a class into another class).

Base class is also known as parent class or super class. The new class that is formed is called derived class. The derived class is also known as sub class or child class. Inheritance is basically used for reducing the overall code size of the program.

Example of Inheritance

// C++ program to demonstrate example of
// simple inheritance

#include <iostream>
using namespace std;

// Base Class
class A {
public:
    void Afun(void);
};

// Function definiion
void A::Afun(void)
{
    cout << "I'm the body of Afun()..." << endl;
}

// Derived Class
class B : public A {
public:
    void Bfun(void);
};
// Function definition
void B::Bfun(void)
{
    cout << "I'm the body of Bfun()..." << endl;
}

int main()
{
    // Create object of derived class - class B
    B objB;
    
    // Now, we can access the function of class A (Base class)
    objB.Afun();
    objB.Bfun();
    
    return 0;
}

Output

I'm the body of Afun()...
I'm the body of Bfun()...

Data Encapsulation

Data encapsulation combines data members and member functions into a single unit that is called class. The advantage of encapsulation is that data cannot access directly. It is only accessible through the member functions of the class.

Data Abstraction

Data abstraction specifies hiding the implementation detail for simplicity. It increases the power of programming language by creating user define data types.

Polymorphism

Polymorphism is basic and important concept of OOPS. Polymorphism specifies the ability to assume several forms. It allows routines to use variables of different types at different times. In C++, an operator or function can be given different meanings or functions. Polymorphism refers to a single function or multi-functioning operator performing in different ways.

Read more:

  1. C++ polymorphism and its types
  2. C++ Class and Objects programs/examples

Related Tutorials

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.