Vous êtes sur la page 1sur 4

Introduction to Classes and Objects

The classes are the most important feature of C++ that leads to Object Oriented programming. Class is a user
defined data type, which holds its own data members and member functions, which can be accessed and used by
creating instance of that class.
The variables inside class definition are called as data members and the functions are called member functions.
For example : Class of birds, all birds can fly and they all have wings and beaks. So here flying is a behavior and
wings and beaks are part of their characteristics. And there are many different birds in this class with different names
but they all posses this behavior and characteristics.
Similarly, class is just a blue print, which declares and defines characteristics and behavior, namely data members
and member functions respectively. And all objects of this class will share these characteristics and behavior.
More about Classes

1. Class name must start with an uppercase letter. If class name is made of more than one word, then first
letter of each word must be in uppercase. Example,

class Study, class StudyTonight etc

2. Classes contain, data members and member functions, and the access of these data members and variable
depends on the access specifiers (discussed in next section).

3. Class's member functions can be defined inside the class definition or outside the class definition.

4. Class in C++ are similar to structures in C, the only difference being, class defaults to private access
control, where as structure defaults to public.

5. All the features of OOPS, revolve around classes in C++. Inheritance, Encapsulation, Abstraction etc.

6. Objects of class holds separate copies of data members. We can create as many objects of a class as we
need.

7. Classes do posses more characteristics, like we can create abstract classes, immutable classes, all this we
will study later.

Objects
Class is mere a blueprint or a template. No storage is assigned when we define a class. Objects are instances of class,
which holds the data variables declared in class and the member functions work on these class objects.
Each object has different data variables. Objects are initialised using special class functions called Constructors.
We will study about constructors later.
And whenever the object is out of its scope, another special class member function called Destructor is called, to
release the memory reserved by the object. C++ doesn't have Automatic Garbage Collector like in JAVA, in C++
Destructor performs this task.
class Abc

int x;

void display(){} //empty function

};

in main()

Abc obj; // Object of class Abc created

Overview of Inheritance
Inheritance is the capability of one class to acquire properties and characteristics from another class. The class whose properties
are inherited by other class is called the Parent or Base or Super class. And, the class which inherits properties of other class is
called Child or Derived or Sub class.
Inheritance makes the code reusable. When we inherit an existing class, all its methods and fields become available in the new
class, hence code is reused.
NOTE : All members of a class except Private, are inherited

Purpose of Inheritance
1. Code Reusability

2. Method Overriding (Hence, Runtime Polymorphism.)

3. Use of Virtual Keyword

Depending on Access modifier used while inheritance, the availability of class members of Super class in the sub class changes. It
can either be private, protected or public.

1) Public Inheritance
This is the most used inheritance mode. In this the protected member of super class becomes protected members of sub class and
public becomes public.
class Subclass : public Superclass

2) Private Inheritance
In private mode, the protected and public members of super class become private members of derived class.
class Subclass : Superclass // By default its private inheritance

3) Protected Inheritance
In protected mode, the public and protected members of Super class becomes protected members of Sub class.
class subclass : protected Superclass

Data abstraction

Data abstraction refers to, providing only essential information to the outside world and hiding their
background details, i.e., to represent the needed information in program without presenting the
details.

Data abstraction is a programming (and design) technique that relies on the separation of interface
and implementation.

Let's take one real life example of a TV, which you can turn on and off, change the channel, adjust
the volume, and add external components such as speakers, VCRs, and DVD players, BUT you do not
know its internal details, that is, you do not know how it receives signals over the air or through a
cable, how it translates them, and finally displays them on the screen.

Thus, we can say a television clearly separates its internal implementation from its external interface
and you can play with its interfaces like the power button, channel changer, and volume control
without having zero knowledge of its internals.

Now, if we talk in terms of C++ Programming, C++ classes provides great level of data abstraction.
They provide sufficient public methods to the outside world to play with the functionality of the object
and to manipulate object data, i.e., state without actually knowing how class has been implemented
internally.

For example, your program can make a call to the sort() function without knowing what algorithm the
function actually uses to sort the given values. In fact, the underlying implementation of the sorting
functionality could change between releases of the library, and as long as the interface stays the
same, your function call will still work.

Data Encapsulation
Encapsulation is the method of combining the data and functions inside a class. This hides the data
from being accessed from outside a class directly, only through the functions inside the class is able
to access the information.
This is also known as "Data Abstraction", as it gives a clear separation between properties of data
type and the associated implementation details. There are two types, they are "function abstraction"
and "data abstraction". Functions that can be used without knowing how its implemented is function
abstraction. Data abstraction is using data without knowing how the data is stored.

Example :
#include <iostream.h> class Add
{
private:
int x,y,r;
public:
int Addition(int x, int y)
{
r= x+y;
return r;
}
void show( )
{ cout << "The sum is::" << r << "\n";}
}s;
void main()
{
Add s;
s.Addition(10, 4);
s.show();
}

Result :
The sum is:: 14
In the above encapsulation example the integer values "x,y,r" of the class "Add" can be accessed only
through the function "Addition". These integer values are encapsulated inside the class "Add".

#include <stdio.h>
#include <iostream.h>
#include <conio.h>
const char ESC = 27; //escape key ASCII code
const int TOLL =10; //toll is 50 cents
class tollBooth
{
private:
unsigned int totalCars; //total cars passed today
double totalCash; //total money collected today
public: //constructor
tollBooth() : totalCars(0), totalCash(0)
{}
void payCar() //a car paid
{ totalCars++; totalCash += TOLL; }
void freeCar() //a car didnt pay
{ totalCars++; }
void display() const //display totals
{cout<< "\nCars=" << totalCars
<< ", cash=" << totalCash
<< endl;
}
};
int main()
{
tollBooth booth1; //create a toll booth
char ch;
cout << "\nPress 0 for each non-paying car,"
<< "\n 1 for each paying car,"
<< "\n Esc to exit the program.\n";
do {
ch = getche(); //get character
if( ch == '0' ) //if its 0, car didnt pay
booth1.freeCar();
if( ch == '1' ) //if its 1, car paid
booth1.payCar();
} while( ch != ESC ); //exit loop on Esc key
booth1.display(); //display totals
return 0;
}

Vous aimerez peut-être aussi