You are on page 1of 8

Basic concepts of OOPS and Structure of C++ program

In this tutorial you will learn about Objects, Classes, Inheritance, Data Abstraction, Data
Encapsulation, Polymorphism, Overloading, and Reusability.

Before starting to learn C++ it is essential that one must have a basic knowledge of the
concepts of Object oriented programming. Some of the important object oriented features
are namely:


Objects
• Classes
• Inheritance
• Data Abstraction
• Data Encapsulation
• Polymorphism
• Overloading
• Reusability

In order to understand the basic concepts in C++, the programmer must have a command
of the basic terminology in object-oriented programming. Below is a brief outline of the
concepts of Object-oriented programming languages:

Objects:
Object is the basic unit of object-oriented programming. Objects are identified by its unique
name. An object represents a particular instance of a class. There can be more than one
instance of an object. Each instance of an object can hold its own relevant data.
An Object is a collection of data members and associated member functions also known as
methods.

Classes:
Classes are data types based on which objects are created. Objects with similar properties
and methods are grouped together to form a Class. Thus a Class represent a set of
individual objects. Characteristics of an object are represented in a class as Properties. The
actions that can be performed by objects becomes functions of the class and is referred to
as Methods.

For example consider we have a Class of Cars under which Santro


Xing, Alto and WaganR represents individual Objects. In this context each Car Object will
have its own, Model, Year of Manufacture, Colour, Top Speed, Engine Power etc., which
form Properties of the Car class and the associated actions i.e., object functions like Start,
Move, Stop form the Methods of Car Class.

No memory is allocated when a class is created. Memory is allocated only when an object is
created, i.e., when an instance of a class is created.

Inheritance:
Inheritance is the process of forming a new class from an existing class or base class. The
base class is also known as parent class or super class, The new class that is formed is
called derived class. Derived class is also known as a child class or sub class. Inheritance
helps in reducing the overall code size of the program, which is an important concept in
object-oriented programming.

Data Abstraction:
Data Abstraction increases the power of programming language by creating user defined
data types. Data Abstraction also represents the needed information in the program without
presenting the details.

Data Encapsulation:
Data Encapsulation combines data and functions into a single unit called Class. When using
Data Encapsulation, data is not accessed directly; it is only accessible through the functions
present inside the class. Data Encapsulation enables the important concept of data hiding
possible.

Polymorphism:
Polymorphism allows routines to use variables of different types at different times. 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.

Overloading:
Overloading is one type of Polymorphism. It allows an object to have different meanings,
depending on its context. When an exiting operator or function begins to operate on new
data type, or class, it is understood to be overloaded.
Reusability:
This term refers to the ability for multiple programmers to use the same written and
debugged existing class of data. This is a time saving device and adds code efficiency to the
language. Additionally, the programmer can incorporate new features to the existing class,
further developing the application and allowing users to achieve increased performance.
This time saving feature optimizes code, helps in gaining secured applications and facilitates
easier maintenance on the application.

The implementation of each of the above object-oriented programming features for C++ will
be highlighted in later sections.

A sample program to understand the basic structure of C++

Sample Code

1.
2. //program to read employee details and to output the data
3.
4. ////////// code begins here /////////////////////////////
5. #include <iostream> //&#8594; Preprocessor directive
6. using namespace std;
7. class employee //&#8594; Class Declaration
8. {
9. private:
10. char empname[50];
11. int empno;
12.
13. public:
14. void getvalue()
15. {
16. cout<<"INPUT Employee Name:";
17. cin>>empname;
18. cout<<"INPUT Employee Number:";
19. cin>>empno;
20. }
21.
22. void displayvalue()
23. {
24. cout<<"Employee Name:"<<empname<<endl;
25. cout<<"Employee Number:"<<empno<<endl;
26. }
27. };
28.
29. main()
30. {
31. employee e1; //&#8594; Creation of Object
32. e1.getvalue();
33. e1.displayvalue();
34. }
35.
36. ///// code ends here //////////////
37.

Fundamental concepts and features


A survey by Deborah J. Armstrong of nearly 40 years of computing literature identified a number of
"quarks", or fundamental concepts, found in the strong majority of definitions of OOP.[13]

Not all of these concepts are to be found in all object-oriented programming languages, and so object-
oriented programming that uses classes is called sometimes class-based programming. In
particular, prototype-based programming does not typically use classes. As a result, a significantly
different yet analogous terminology is used to define the concepts of object and instance.

Class
A Class is a user defined data type which contains the variables, properties and methods in it. 'A class
defines' the abstract characteristics of a thing (object), including its characteristics
(its attributes, fields or properties) and the thing's behaviors (the things it can do, or
methods, operations or features). One might say that a class is a blueprint or factory that describes the
nature of something. For example, the class Dog would consist of traits shared by all dogs, such as breed
and fur color (characteristics), and the ability to bark and sit (behaviors). Classes
provide modularity and structure in an object-oriented computer program. A class should typically be
recognizable to a non-programmer familiar with the problem domain, meaning that the characteristics of
the class should make sense in context. Also, the code for a class should be relatively self-contained
(generally using encapsulation). Collectively, the properties and methods defined by a class are
called members.

Instance
One can have an instance of a class; the instance is the actual object created at run-time. In programmer
vernacular, the Lassie object is an instance of the Dog class. The set of values of the attributes of a
particular object is called its state. The object consists of state and the behavior that's defined in the
object's classes.

Method
Method is a set of procedural statements for achieving the desired result. It performs different kinds of
operations on different data types. In a programming language, methods (sometimes referred to as
"functions") are verbs. Lassie, being a Dog, has the ability to bark. So bark() is one of Lassie's
methods. She may have other methods as well, for
example sit() or eat() or walk() or save(Timmy). Within the program, using a method usually
affects only one particular object; all Dogs can bark, but you need only one particular dog to do the
barking.

Message passing
"The process by which an object sends data to another object or asks the other object to invoke a
method."[13] Also known to some programming languages as interfacing. For example, the object
called Breeder may tell the Lassie object to sit by passing a "sit" message which invokes Lassie's "sit"
method. The syntax varies between languages, for example: [Lassie sit] in Objective-C. In Java,
code-level message passing corresponds to "method calling". Some dynamic languages use double-
dispatch or multi-dispatch to find and pass messages.

Inheritance
Inheritance is a process in which a class inherits all the state and behavior of another class. this type of
relationship is called child-Parent or is-a relationship. "Subclasses" are more specialized versions of a
class, which inherit attributes and behaviors from their parent classes, and can introduce their own.

For example, the class Dog might have sub-classes called Collie, Chihuahua,
and GoldenRetriever. In this case, Lassie would be an instance of the Collie subclass. Suppose
the Dog class defines a method called bark() and a property called furColor. Each of its sub-classes
(Collie, Chihuahua, and GoldenRetriever) will inherit these members, meaning that the
programmer only needs to write the code for them once.

Each subclass can alter its inherited traits. For example, the Collie subclass might specify that the
default furColor for a collie is brown-and-white. The Chihuahua subclass might specify that
the bark() method produces a high pitch by default. Subclasses can also add new members.
The Chihuahua subclass could add a method called tremble(). So an individual chihuahua instance
would use a high-pitched bark() from the Chihuahua subclass, which in turn inherited the
usual bark() from Dog. The chihuahua object would also have the tremble() method,
but Lassie would not, because she is a Collie, not a Chihuahua. In fact, inheritance is an "a... is a"
relationship between classes, while instantiation is an "is a" relationship between an object and a
class: a Collie is a Dog ("a... is a"), but Lassie is a Collie ("is a"). Thus, the object
named Lassie has the methods from both classes Collie and Dog.
Multiple inheritance is inheritance from more than one ancestor class, neither of these ancestors being an
ancestor of the other. For example, independent classes could define Dogs and Cats, and
a Chimera object could be created from these two which inherits all the (multiple) behavior of cats and
dogs. This is not always supported, as it can be hard to implement.

Abstraction
Abstraction is simplifying complex reality by modeling classes appropriate to the problem, and working at
the most appropriate level of inheritance for a given aspect of the problem.

For example, Lassie the Dog may be treated as a Dog much of the time, a Collie when necessary to
access Collie-specific attributes or behaviors, and as an Animal (perhaps the parent class of Dog)
when counting Timmy's pets.

Encapsulation
Encapsulation conceals the functional details of a class from objects that send messages to it.

For example, the Dog class has a bark() method. The code for the bark() method defines exactly how
a bark happens (e.g., byinhale() and then exhale(), at a particular pitch and volume).
Timmy, Lassie's friend, however, does not need to know exactly how she barks. Encapsulation is
achieved by specifying which classes may use the members of an object. The result is that each object
exposes to any class a certain interface — those members accessible to that class. The reason for
encapsulation is to prevent clients of an interface from depending on those parts of the implementation
that are likely to change in the future, thereby allowing those changes to be made more easily, that is,
without changes to clients. For example, an interface can ensure that puppies can only be added to an
object of the classDog by code in that class. Members are often specified
as public, protected or private, determining whether they are available to all classes, sub-classes or
only the defining class. Some languages go further: Java uses the default access modifier to restrict
access also to classes in the same package, C# and VB.NET reserve some members to classes in the
same assembly using keywords internal (C#) orFriend (VB.NET), and Eiffel and C++ allow one to
specify which classes may access any member. This is not encapsulation

(Subtype) polymorphism
Polymorphism allows the programmer to treat derived class members just like their parent class's
members. More precisely, Polymorphism in object-oriented programming is the ability
of objects belonging to different data types to respond to calls of methods of the same name, each one
according to an appropriate type-specific behavior. One method, or an operator such as +, -, or *, can be
abstractly applied in many different situations. If a Dog is commanded to speak(), this may elicit
a bark(). However, if a Pig is commanded to speak(), this may elicit an oink(). Each subclass
overrides the speak() method inherited from the parent class Animal.

Decoupling
Decoupling allows for the separation of object interactions from classes and inheritance into distinct layers
of abstraction. A common use of decoupling is to polymorphical decouple the encapsulation, which is the
practice of using reusable code to prevent discrete code modules from interacting with each other.
However, in practice decoupling often involves trade-offs with regard to which patterns of change to favor.
The science of measuring these trade-offs in respect to actual change in an objective way is still in its
infancy.

1) Object-Instance of the class


2) Classes-Combination of Data members and member functions
3) Data Abstraction-Abstracting the data and functions from
Unwanted users.
4) Data Encapsulation-Wrapping up of Data members and member
functions.
5) Inheritance-Passing the values from one object to another
object.
6)Message Passing-Messages can possible from one object to
another object
7) Polymorphism-it’s like function over loading.
Poly-Many, morphism-Forms
8) Robust and Securisam-it’s for security purpose

1. OBJECTS:
An object is an abstraction of a real world entity. It may
represent a person,a place a number and icons or something
else that can be modeled. Any data in an object occupy some
space in memory and can communicate with each other.
2. CLASSES:
A class is a collection of objects having common
features .It is a user defined data types which has data
members as well functions that manipulate these data’s.
3. ABSTRACTION:
It can be defined as the separation of unnecessary
details or explation from system requirements so as to
reduce the complexities of understanding requirements.
4. ENCAPTULATION:
It is a mechanism that puts the data and function together.
It is the result of hiding implementation details of an
object from its user .The object hides its data to de
accessed by only those functions which are packed in the
class of that object.
5. INHERITANCE:
It is the relationship between two classes of object such
that one of the classes ,the child takes all the relevant
features of other class -the parent.
Inheritance bring about reusablity.
6. POLYMORPHISM:
polymorphism means having many forms that in a single
entity can takes more than one form. Polymorphism is
implemented through operator overloading and function
Overloading.
7. DYNAMIC BINDING:
Dynamic binding is the process of resolving the function to
be associated with the respective functions calls during
their runtime rather than compile time.
8. MESSAGE PASSING:
Every data in an object in oops that is capable of
processing request known as message .All object can
communicate with each other by sending message to each other

You might also like