You are on page 1of 6

Object-Oriented Programming Language

by
Venkatesh.N
Swarna Bharathi Institute of Technology and Sciences
Khammam
Introduction
Object-oriented techniques view the world as consisiting of Objects. Objects are key to understanding
object-oriented technology. Look around right now and you’ll find many examples of real-world objects:
your pen, your bench, piece of chalk, your bicycle, your dog.1
Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name,
color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear,
current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying
brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms
of object-oriented programming.
A more common example is every SBIT student has rollno,some hobbies and they are goodat something.
here state(rollno and hobbies), behavior(goodat).
A broad definition of a Object-Oriented.
A language or technique is object-oriented if and only if it directly supports
[1] Abstraction [2] Inheritance [3] RuntimeP olymorphism
examples: Ada95, Beta,C++, CLOS, Eiffel, Simula, Smalltalk,java etc. are object-oriented languages
Abstraction
An Abstraction is a way of representing a group of related things by a single thing which expresses their
similarities and suppresses their difference. (or)
By Abstraction, we mean identifying different but similar behaviour of related entities through a name.
example: Consider a more common statement often made about animals - ” all animals eat ...”
clearly, the verb eat here represents an abstarct behavior about animals.
In C++ we can get abstraction by using class or struct or union.
example: Every SBITSTUDENT has rollno, Every SBITSTUDENT has some hobbies and Every SBIT-
STUDENT is goodat... goodat represents abstract behaviour about SBITSTUDENT. because neither the
speaker nor the listner bothered where they are good just they are goodat.Above three statements gives
abstarct behaviour about a SBITSTUDENT. we can group with a name SBITSTUDENT.
class SBITSTUDENT
{
char rollno[10];
char hobbies[20];
void goodat(){ cout<<"good at"<<hobbies;}
};
SBITSTUDENT anil;
SBITSTUDENT jyotshna;
anil is an instance of the class of objects known as SBITSTUDENT.
jyotshna is an instance of the class of objects known as SBITSTUDENT.
1
Credit where the Credit is due
Dedicated to my mother

1
Basic understanding is something like this.
A class is a blueprint for an object.
the blueprint specifies the attributes or data and methods or functions or operations of the class.
Object is an instance of a class.
Because object is instantiated(created) from the description provided by its class.
example: when a civil-engineer gives you a plan to construct buildings. By using that plan if you con-
struct buildings x,y. Plan is a blueprint like a class.
x,y are objects of a Plan. Because x,y are created from the description provided by Plan.
Each object has its own values for the attributes or data.
Each object shares the implementation of a class’s methods or functions.
Look at the SBIT class declaration, we have a memberfunction called goodat()
if we call jyotshna.goodat() then it has to print jyotshna hobbies.
if we call anil.goodat() then it has to print anil hobbies.
A possible question may arises: ” how does the member function come to know the instance(object) which
caused its latest invocation?” because each object shares the implementation of class’s member function.
Although at the lowest level this is basically an implementation dependent feature, usually an invisible
parameter representing the instance of the class making the invocation( or a pointer, as in c++) is passed to
the function. The compiler ensures that all the refrences to the member variables in the scope of a member
function are actually directed to those of the invisible parameter. In c++ you can refrence the invisible
parameter by using this keyword.
this is a pointer which stores the address of the current object which called the memberfunction.

Class Member Visibility

Any member of a class may have three types of visibility, indicated by three types of access specifiers
keywords — private, protected and public. Member of a class with private visibilities are accessi-
ble(visible) only within the class or from the extended scopes of the class. Member of a class with
protected visibilities are accessible(visible) from the extended scopes of the class or classes derived from
it. Member of a class with public visibilities are accessible(visible) from everywhere.

C++ class provides extra facilities called Encapsulation, Protection, Information-Hiding.


Encapsulation is a process of binding data and the codes that operates on the data into a single entity.
This keeps the data safe from outside interface and misuse. or
Encapsulation is the inclusion within a program object of all the resources need for the object to function
- basically, the method and the data. The object is said to publish its interfaces. Other objects adhere to
these interfaces to use the object without having to be concerned with how the object accomplishes it. The
idea is ”don’t tell me how you do it; just do it”. An object can be thought of as a selfcontained atom. The
object interface consists of public methods and instantiate data.
Protection and information hiding are techniques used to accomplish encapsulation of an object. Protec-
tion is when you limit the use of class data or methods. Information hiding is when you remove data,
methods or code from a class’s public interface in order to refine the scope of an object. So how are
these three concepts implemented in C++? You’ll remember that C++ classes have a public, protected
and private interface. Moving methods or data from public to protected or to private, you are hiding the
information from the public or protected interface. If you have a class A with one public integer data
member d, then the C++ definition would be...

2
class A
{
public:
integer d;
};

If you moved that data member from the public scope of the private scope, then you would be hiding the
member. Better said, you are hiding the member from the public interface.

class A
{
private:
integer d;
};

It is important to note that information hiding are not the same as encapsulation. Just because you protect
or hide methods or data, does not mean you are encapsulating an object. But the ability to protect or
hide methods or data, provide the ability to encapsulate an object. You might say that encapsulating is
the proper use of protection and information hiding. As an example, if I used information hiding to hide
members that should clearly be in the public interface, then I am using information hiding techniques, but
I am not encapsulating the class. In fact, I am doing the exact opposite (unencapsulating the class). Do
not get the idea that encapsulation is only information hiding. Encapsulation is a lot more. Protection
is another way of encapsulating a class. Protection is about adding methods and data to a class. When
you add methods or data to a class, then you are protecting the methods or data from use without first
having an object of the class. In the previous example, the data member d cannot be used except as a data
member of an object of class A. It is being protected from use outside of this scenario.

Constructor and Destructor

C++ provides mechanisms for ensuring that your objects are properly initialized before they are used.
As your objects go in and out of scope, memory is allotted for them and that memory is then initialized.
C++ provides a special member function for the initialization of an object, called the constructor.
The constructor is responsible for turning the raw memory allotted to an object into a usable object.
Constructors come in many forms. There are default constructors, copy constructors, and other construc-
tors that take different arguments.
A constructor is called whenever an object is created. Objects can be created: as a global variable, as a
local variable, through explicit use of the new operator, through an explicit call of a constructor, or as a
temporary object. Constructors are also called when an object is created as part of another object.
The default constructor is a constructor that takes no arguments. The default constructor for a class X has
the form X::X(). A constructor which has all default arguments, X::X(const int x=0), for example, is also
a default constructor, since it can be called with no arguments.
A copy constructor is a special constructor that can be called to copy an object. The copy constructor for
class X has the form X::X(const X&).The copy constructor is called more often behind the scenes when-
ever the compiler needs a copy of an object. These objects, appropriately called temporaries, are created
and destroyed as they are needed by the compiler. The most common place this occurs is during function
calls, so that the semantics of call-by-value can be preserved.( refer class note book for examples of
when copy constructer is called )

3
Similarly, when your object goes out of scope, the memory used by the object must be reclaimed. C++
provides a special member function, called the destructor, that is called whenever your object is destroyed
so that you may perform any clean-up processing, such as freeing memory or other system resources ob-
tained by the object. A destructor for class X has the form X::∼X().
Static class members
Sometimes a single value for a data member applies to all objects of the class. In this case, it would be
inefficient to store the same value in every object of the class. That can be avoided by declaring the data
member to be static. This is done by including the static keyword at the begining of the variable’s decla-
ration. It also variable to be defined globally. refer class note book for syntax i.e how to declare,how
to access
ex: Look at SBITStudent class declaration, we can add datamember called tagcolor. As we know tag-
color of every SBIT student is same. instead of creating seperate copy for every object we can make it as
static so that only one copy exists that will be shared by all objects. One thing we can say about this is it’s
actually a property of a class rather than individual objects.
Friends of a class
Non-member functions and other classes may be declared as a friends of a class. such entities are provided
with privilege to access private members of a class. Friends must be declared within the declaration scope
of the class with the special declaration directive friend. All member functions of a friend class to a given
class acquire the privilege to access private members of the latter. Friendship is not commutative.

examples:
1.class A
{
int x;
friend void fun();
};
void fun() //here fun is global or outside function
{
//now fun can access the private members of A
// i.e x by creating object of A
}
2. class B{
fun(){ //fun can access the private members of A}
};
class A{
int x;
friend B::fun();
};
3. class A{ int x;
friend class B;
};
class B{
//all member functions of B can access the private members of A.
};

4
Operator overloading

When we define a class, we are actually creating new type. Most of the c++ operators can be overloaded
to apply to our new class type. All operators except .( struct, class, union member), .*( pointer to a
member), ::( scope resolution), ?:( conditional) may be overloaded. If an operator used as either a unary
operator or a binary operator each may be overloaded separately. An operator may be overloaded using
either a non-static member function or a global function that is a friend of a class. A global function
must have at least one parameter that is of class type or a reference to class type. If a unary operator is
overloaded using a member function, it takes no arguments, if it is overloaded using a global function, it
takes one argument. If a binary operator is overloaded using a member function, it takes one argument,if
it is overloaded using a global function, it takes two arguments.
The keyword operator followed by the operator symbol name is known as the operator function name,
and acts like normal function name in the redefinition of the operator. An operator function called with
arguments behaves like an operator working on its operand in an expression. However, a redefined opera-
tor function cannot alter the number of arguments( arity), precedence and associativity rules that apply to
normal operator usage. refer class note book for examples.

namespace and using directive

A namespace is an abstract environment created to hold a logical grouping of unique identifiers or symbols
(i.e., names). An identifier defined in a namespace is associated with that namespace. The same identifier
can be independently defined in multiple namespaces. That is, the meaning associated with an identifier
defined in one namespace may or may not have the same meaning as the same identifier defined in another
namespace. ex: Praveen doing b.tech from SBIT and his roll no is 552.Ajay doing b.tech from SBCE and
his roll no is 552. The reason Praveen and Ajay can be identified by the same ID number is because they
are doing b.tech from different colleges. The different colleges in this case would symbolize different
namespaces.
In large computer programs or documents it is not uncommon to have hundreds or thousands of identifiers.
Namespaces provide a mechanism for hiding local identifiers. They provide a means of grouping logically
related identifiers into corresponding namespaces, thereby making the system more modular.
the keyword using is used to introduce a name from a namespace into the current declarative region.
All the files in the C++ standard library declare all of its entities within the std namespace. That is why we
have generally included the using namespace std; statement in all programs that used any entity defined
in iostream( cout or cin or endl etc.).2

2
Credit where the credit is due
http://java.sun.com/docs/books/tutorial/java/concepts/index.html
http://www.glenmccl.com/ns comp.htm
http://www.acm.org/crossroads/xrds1-4/ovp.html

5
NON-OOP SPECIFIC SYNTAX EXTENSIONS IN C++
• Primitive datatypes bool, wchar t.
• Local-Variables can be declared anywhere with in the body of a function Except one case.
– No local variable is declared with initialization with in the scope of a region that may be
bypassed. Possible cases are With in switch case statement, program using goto may skip the
declaration but it lies in the same scope.
• Reference variable
syntax : type &aliastox=x; both x,aliastox should be of the same type
• Operators new, delete ( read page no. 12-16 from texbook-1 3 ).
• Functions
– Function declaration interpretation i.e function identified by using it’s signature
– Default parameters A function declaration may specify default values for one or more of its
parameters.A parameter is indicated as default parameter by assigning a constant value(by
using = operator) to it. In the declaration of function with several parameters, there should
not be a default parameter to the left of a non-default one. In otherwords, parameters must be
marked default from right to left.
– Stronger type checking i.e predeclaration of functions before use and actual parameters passed
during a function call must match in type and number with the declared function header with
two exceptions implicit conversion and default parameters are allowed.
– Parameter passing methods
1. Call-by-value
2. Call-by-address
3. Call-by-Reference and const-Reference
– Function overloading : C++ enables several functions of the same name to be defined, as long
as these functions have different sets of parameters (at least as far as their types are concerned).
This capability is called function overloading. When an overloaded function is called, the
C++ compiler selects the proper function by examining the number, types and order of the
arguments in the call. Function overloading is commonly used to create several functions of
the same name that perform similar tasks but on different data types.
for more detail about function overload resolution read pageno. 456-458 from textbook-2 4
– Inline functions : A function call involves substantial overhead. Extra time and space have to
be used to invoke the function, pass parameters to it, allocate storage for its local variables,
store the current variables and the location of execution in the main program,etc. In some
cases, it is better to avoid all this by specifying the function to be inline. This tells the compiler
to replace each call to the function with explicit code for the function. From a programmer
point view , an inline function appears the same as an ordinary function, except for the use of
the inline specifier.

3
Data structures, Algorithms and Applications in C++ by SARTAJ SAHNI
4
C++ Primer by Stanley B.Lippman

You might also like