Concept of Class and Object in C++
Class -A class is a blue print for creating objects.
Definition: A class is a group of objects which show a common property.
OR
A class is a collection of objects of similar type.Once a class is defined, any number of objects can be created which belong to that class.For example – Define a class to print the values of two numbers.
#include<iostream.h>
class HelloWorld
{
int one;
int two;
public:
void setdata()
{
cout<<”enter two numbers”;
cin>>one>>two;
}
void putdata()
{
cout<<one<<endl<<two;
}
};
int main()
{
HelloWrold obj;
Obj.setdata();
Obj.putdata();
return 0;
}
Classes are generally declared using the keyword class, with the following format:
class class_name
{
access_specifier_1:
member;
access_specifier_2:
member;
…
};
Object -It may be defined as identifiable identity with some characteristics and behavior.
Syntax of creating Object:
Class_name Object_Name;
If we have class named AB then the Object of this class can be created using above syntax as
AB Obj;
Abstract class
An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function.
You declare a pure virtual function by using a pure Specifier
(= 0) in the declaration of a virtual member function in the class declaration.
The following is an example of an abstract class:
class AB {
public:
virtual void f() = 0; //Pure virtual Function
};
Concrete class – It is a derived class that implement all the missing functionality of a abstract class.
The following is an example of an concrete class:
class CD : public AB //Here class AB is abstract class
{ // Declare Above
public:
CD()
{ // set up the CD }
virtual f() // implementation of f() of class AB
{
/* do stuff of f() */
}
};
Advantages and disadvantages of Object Oriented Programming
Advantages –
- Re-usability of code.
- Ease of redesign and extension.
- Data can be divided as public and private.(data protection)
- Program development becomes easy due to increased modularity.(abstraction and encapsulation)
Disadvantages –
- The relation among classes become artificial at times
- The object oriented programming design id difficult.
- OOP is a high level concept so takes more time to execute as many routines run behind at the time of execution.
- Offers less number of functions as compared to low level programming which interacts directly with hardware.