Objects and Classes
ABSTRACT
Object is a concept, abstraction, or thing with crisp boundaries and meaning for the problem at
hand .Objects serves two purposes: they promote understanding of the real world and provide a
practical basis for computer implementation. Decomposition of a problem in to objects depends
on judgment and nature of the problem. All objects have identity and are distinguishable .Two
apples with the same color, shape, and texture are still individual apples.
An object class describes a group of objects with similar properties, common behavior, common
relationships to the other objects and common semantics. Person, company animal, process and
window are all object classes. Object in a class have the same attributes and behavior pattern.
The major motivating factor in the invention of object oriented approach is to remove some of
the flaws encountered in the procedural approach .oops treats data as a critical element in the
program development and doesn’t allow it to flow freely around the system .It ties the data more
closely by the functions that operate on it.OOP allows decomposition of a problem in to number
of entities called objects and then builds data and functions around these objects.
DEPT OF MCA BEC,BGK Page 1
Objects and Classes
INTRODUCTION
An object Class defines a group of objects with similar common behavior, properties, and
common relationships to the other objects with common semantics. Object class has same
attributes and behavior pattern. The main motivating aim of object oriented approach is to get rid
of the flaws encountered.
Object oriented programming – As the name suggests uses objects in programming. Object
oriented programming aims to implement real world entities like inheritance, hiding,
polymorphism etc in programming. The main aim of OOP is to bind together the data and the
functions that operate on them so that no other part of code can access this data except that
function.
Let us learn about different characteristics of an Object Oriented Programming language:
Object: Objects are basic run-time entities in an object oriented system, objects are instances of a
class these are defined user defined data types.
DEPT OF MCA BEC,BGK Page 2
Objects and Classes
Object
Any entity that has state and behavior is known as an object. For example: chair, pen, table,
keyboard, bike etc. It can be physical and logical.
Class
Collection of objects is called class. It is a logical entity.
Inheritance
When one object acquires all the properties and behaviors of parent object i.e. known as
inheritance. It provides code reusability. It is used to achieve runtime polymorphism.
Polymorphism
When one task is performed by different ways i.e. known as polymorphism. For example: to
convince the customer differently, to draw something e.g. shape or rectangle etc.
In C++, we use Function overloading and Function overriding to achieve polymorphism.
Abstraction
Hiding internal details and showing functionality is known as abstraction. For example: phone
call, we don't know the internal processing.
In C++, we use abstract class and interface to achieve abstraction.
Encapsulation
Binding (or wrapping) code and data together into a single unit is known as encapsulation. For
example: capsule, it is wrapped with different medicines.
DEPT OF MCA BEC,BGK Page 3
Objects and Classes
Advantage of OOPs over Procedure-oriented programming language .
OOPs makes development and maintenance easier where as in Procedure-oriented
programming language it is not easy to manage if code grows as project size grows.
OOPs provide data hiding whereas in Procedure-oriented programming language a global
data can be accessed from anywhere.
OOPs provide ability to simulate real-world event much more effectively. We can
provide the solution of real word problem if we are using the Object-Oriented
Programming language.
DEPT OF MCA BEC,BGK Page 4
Objects and Classes
Object:-
An object is nothing but self-contained components which consists of methods and properties to
make a particular type of data useful. Object determines the behavior of the class. When you
send a message to an object, you are asking the object to invoke or execute one of its methods.
From a programming point of view, an object can be a data structure, a variable or a function. It
has a memory location allocated. The object is designed as class hierarchies.
Object-oriented programming shifts the focus of attention to the objects, that is, to the aspects on
which the problem is centered. A program designed to maintain bank accounts would work with
data such as balances, credit limits, transfers, interest calculations, and so on. An object
representing an account in a program will have properties and capacities that are important for
account management. OOP objects combine data (properties) and functions (capacities). A class
defines a certain object type by defining both the properties and the capacities of the objects of
that type. Objects communicate by sending each other “messages,” which in turn activate
another object’s capacities.
DEPT OF MCA BEC,BGK Page 5
Objects and Classes
Creating Objects
An object is a variable of a class type, also referred to as an instance of the class. When an object
is created, memory is allocated to the data members and initialized with suitable values.
Example: strings ("I am a string");
In this example the object s, an instance of the standard class string (or simply a string), is
defined and initialized with the string constant that follows. Objects of the string class manage
the memory space required for the string themselves. In general, there are several ways of
initializing an object of a class. A string can thus be initialized with a certain number of identical
characters, as the example on the opposite page illustrates.
Calling Methods using Object
All the methods defined as public within the corresponding class can be called for an object. In
contrast to calling a global function, a method is always called for one particular object. The
name of the object precedes the method and is separated from the method by a period.
Example: s.length(); // object.method();
The method length() supplies the length of a string, i.e. the number of characters in a string.
These results in a value of 13 for the string s defined above.
DEPT OF MCA BEC,BGK Page 6
Objects and Classes
Passing an Object as argument
To pass an object as an argument we write the object name as the argument while calling the
function the same way we do it for other variables.
Syntax:-
Function_name(object name);
Example:- C++ program to show passing of objects to a function
#include <bits/stdc++.h>
using namespace std;
class Example {
public:
int a;
// This function will take
// an object as an argument
void add(Example E)
a = a + E.a;
};
// Driver Code
int main()
// Create objects
Example E1, E2;
// Values are initialized for both objects
DEPT OF MCA BEC,BGK Page 7
Objects and Classes
E1.a = 50;
E2.a = 100;
cout << "Initial Values \n";
cout << "Value of object 1: " << E1.a
<< "\n& object 2: " << E2.a
<< "\n\n";
// Passing object as an argument
// to function add()
E2.add(E1);
// Changed values after passing
// object as argument
cout << "New values \n";
cout << "Value of object 1: " << E1.a
<< "\n& object 2: " << E2.a
<< "\n\n";
return 0;
Output:-
Initial Values
Value of object 1:50
& object 2:100
New values
Values of object 1:50
& object 2:150
DEPT OF MCA BEC,BGK Page 8
Objects and Classes
Class:-
A class is an entity that determines how an object will behave and what the object will contain.
In other words, it is a blueprint or a set of instruction to build a specific type of object.
Syntax:-
Class <class_name>{
Filed;
Method; }
A Class is a user defined data-type which has data members and member functions.
Data members are the data variables and member functions are the functions used to
manipulate these variables and together these data members and member functions defines
the properties and behavior of the objects in a Class.
How to define a class in C++?
A class is defined in C++ using keyword class followed by the name of class. The body of class
is defined inside the curly brackets and terminated by a semicolon at the end.
Class className
//some data
//some functions
DEPT OF MCA BEC,BGK Page 9
Objects and Classes
Keywords: private and public
You may have noticed two keywords: private and public in the above example.
The private keyword makes data and functions private. Private data and functions can be
accessed only from inside the same class.
The public keyword makes data and functions public. Public data and functions can be accessed
out of the class.
Here, data1 and data2 are private members where as function1() and function2() are public
members.
If you try to access private data from outside of the class, compiler throws error. This feature
in OOP is known as data hiding.
DEPT OF MCA BEC,BGK Page 10
Objects and Classes
Defining Class and Declaring Objects
A class is defined in C++ using keyword class followed by the name of class. The body of class
is defined inside the curly brackets and terminated by a semicolon at the end.
#include <iostream>
using namespace std;
class Student {
public:
int id;//data member (also instance variable)
string name;//data member(also instance variable)
};
int main() {
Student s1; //creating an object of Student
s1.id = 201;
s1.name = "Sonoo Jaiswal";
cout<<s1.id<<endl;
cout<<s1.name<<endl;
return 0;
}
Output:
201
Sonoo Jaiswal
DEPT OF MCA BEC,BGK Page 11
Objects and Classes
How to Creating Objects and Allocate Memory for Objects in C++
Once a class is defined, it can be used to create variables of its type known as objects. The
relation between an object and a class is the same as that of a variable and its data types.
The syntax for declaring an object is
class_name = object_list;
where,
class_name = the name of the class
object_list = a comma-separated list of objects
To understand the concept of instantiation, consider this example.
Example : Declaring objects of a class
class book
II body of the class
};
int main ()
book bookl, book2, book3; //objects of class book
return 0;
DEPT OF MCA BEC,BGK Page 12
Objects and Classes
In this example, three objects namely, book1, book2 and book3 of the class book have been
created in main ().
Note that the syntax for the declaration of objects of a particular class is same as that for the
declaration of variables of a built-in data type. To understand this concept, consider these
statements.
int a,b,c; // declaration of variables
book bookl, book2, book3; // declaration of objects
Like structures, objects of a class can also be declared at the time of defining the class as shown
here.
class class_name
Object_list;
Hence, the objects of the class book can also be declared with the help of these statements.
class book
// body of class
}bookl,book2,book3; // declaration of objects
Memory Allocation for Objects
Before using a member of a class, it is necessary to allocate the required memory space to
that member. The way the memory space for data members and member functions is allocated is
different regardless of the fact that both data members and member functions belong to the same
class.
DEPT OF MCA BEC,BGK Page 13
Objects and Classes
The memory space is allocated to the data members of a class only when an object of the class is
declared, and not when the data members are declared inside the class. Since a single data
member can have different values for different objects at the same time, every object declared for
the class has an individual copy of all the data members.
On the other hand, the memory space for the member functions is allocated only once when the
class is defined. In other words, there is only a single copy of each member function, which is
shared among all the objects. For instance, the three objects, namely, book1, book2 and book3 of
the class book have individual copies of the data member’s title and price. However, there is only
one copy of the member functions getdata () and putdata () that is shared by all the three objects.
DEPT OF MCA BEC,BGK Page 14
Objects and Classes
Conclusion
Object-oriented programming (OOP) is a computer programming that uses objects, data
structures that consists of methods and data fields with their interactions to design computer
programs and applications. This technique includes many features like data abstraction,
encapsulation, polymorphism, and inheritance.
DEPT OF MCA BEC,BGK Page 15
Objects and Classes
References
http://ecomputernotes.com/cpp/classes-in-c/creating-objects-and-allocate-memory-for-objects
https://www.tutorialspoint.com/cplusplus/cpp_pointers.htm
DEPT OF MCA BEC,BGK Page 16