explain oops concepts with examples?

Answer Posted / rajasekharareddy

* 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.

Is This Answer Correct ?    12 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is java abstraction with example?

556


What’s the difference between the methods sleep() and wait()?

543


What is the difference between break and continue statements?

547


Difference between notify() method and notifyall() method in java?

550


What is scope of a variable?

603






Is it possible for yielded thread to get chance for its execution again ?

551


Which is faster set or list in java?

530


What does localhost mean?

498


What are synchronized methods ?

617


What is string in java?

561


What is abstract class constructor called?

569


Is null false in java?

706


What is difference between hashset and hashmap?

564


Where can I find jdk in my computer?

466


3.2 Consider the following class: public class Point { protected int x, y; public Point(int xx, int yy) { x = xx; y = yy; } public Point() { this(0, 0); } public int getx() { return x; } public int gety() { return y; } public String toString() { return "("+x+", "+y+")"; } } Say you wanted to define a rectangle class that stored its top left corner and its height and width as fields. 3.2.1 Why would it be wrong to make Rectangle inherit from Point (where in fact it would inherit the x and y coordinates for its top left corner and you could just add the height and width as additional fields)? (1) 8 Now consider the following skeleton of the Rectangle class: public class Rectangle { private Point topLeft; private int height, width; public Rectangle(Point tl, int h, int w) { topLeft = tl; height = h; width = w; } public Rectangle() { this(new Point(), 0, 0); } // methods come here } 3.2.2 Explain the no-argument constructor of the Rectangle class given above. 3.2.3 Write methods for the Rectangle class to do the following: • a toString() method that returns a string of the format "top left = (x, y); height = h; width = w " where x, y, h and w are the appropriate integer values. • an above() method that tests whether one rectangle is completely above another (i.e. all y values of the one rectangle are greater than all y values of the other). For example, with the following declarations Rectangle r1 = new Rectangle(); Rectangle r2 = new Rectangle(new Point(2,2), 1, 4); the expression r2.above(r1) should give true, and r2.above (r2) should give false. (You can assume that the height of a rectangle is never negative.) (2) (5)

2428