Objects and Classes: Dept of Mca Bec, BGK
Objects and Classes: Dept of Mca Bec, BGK
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.
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
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
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.
Abstraction
Hiding internal details and showing functionality is known as abstraction. For example: phone
call, we don't know the internal processing.
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.
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.
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.
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.
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.
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.
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);
#include <bits/stdc++.h>
class Example {
public:
int a;
// an object as an argument
void add(Example E)
a = a + E.a;
};
// Driver Code
int main()
// Create objects
E1.a = 50;
E2.a = 100;
<< "\n\n";
// to function add()
E2.add(E1);
// object as argument
<< "\n\n";
return 0;
Output:-
Initial Values
New values
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.
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
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.
If you try to access private data from outside of the class, compiler throws error. This feature
in OOP is known as data hiding.
#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
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.
class_name = object_list;
where,
class book
};
int main ()
return 0;
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.
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
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.
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.
Conclusion
References
http://ecomputernotes.com/cpp/classes-in-c/creating-objects-and-allocate-memory-for-objects
https://www.tutorialspoint.com/cplusplus/cpp_pointers.htm