Object in C /C++ with programming example
Introduction of the Object :
object in c/c++ A data type is used to declare a variable. A variable of a data type is also known as the instance or case of that data type. Each variable has a unique name but each variable follows the rules of its data type. When a variable of a data type is declared, some space is reserved for it in the memory.
A class is also like a data type. It is therefore used to declare variables or instances. The variable or instances of a class is called object in c. A class may contain several data items and functions. Thus, the object of a class consists of both the data members and the member functions of the class. The combining of both the data and the functions into one unit is called data encapsulation.
An object in c/c++ represents data members of a class in the memory. Each object of a class has a unique name. the name of an object differentiates it from other objects of the same class. The values of data members of different objects may be different or same. The values of data members in an object are known as the state of the object.
The functions in an object are called the member functions. They are also known as methods. The member functions are used to process and access data of the object in c/c++.
For example, the data from an object is accessed and processed through the member function of the object. It cannot directly read the data in the object. The data is hidden for other objects of the program. This is called data hiding. The data hiding and data encapsulation are the main features of object-oriented programming.
Amazon Purchase Links:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
Declaring Object in c/c++ of a Class:
The objects to a class are declared in a similar way as the variables of any data or structure type are declared.
When a class is defined, no space is reserved for it in the memory. It only provides information about how its object will look like. When an object of a class is declared, memory space is reserved for that object.
The syntax to create an object in c/c++ of class type is :
Class_name object_name separated by commas;
For example, to define an object data of class student, the statement is written as :
student data;
In the above statement, one object data is declared of class “student”. It is the declaration of an object that actually creates an object in the memory.
Calling Member Functions of the object in c/c++:
The member function of a class is called in a similar way as member or data item of a structure is called. The member function is called through an object of the class. The dot operator is used. The dot operator connects the object name and the member functions.
The general syntax to call a member function is:
Object_name.member_function();
For example, if “data” is the name of the object and “show_data” is the member function then the member function is called as shown below:
data.show_data();
the dot operator is also called the class member access operator.
Only those member functions can be accessed from outside the class with the dot operator that has been declared as public.
Example how to call member function in of the object in c/c++ programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <iostream> using namespace std; class sum { private: int n, m; public: void get(int a, int b) { n=a; m=b; } void display(void) { cout<<"Sum is : "<<n+m; } }; int main() { sum s; int x, y; cout<<"Enter first number: "; cin>>x; cout<<"Enter second number: "; cin>>y; s.get(x,y); s.display(); return 0; } |
Output
1 2 3 |
Enter first number: 3 Enter second number: 4 Sum is : 7 |
Example calling member function of Object in c/c++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <iostream> using namespace std; class emp { private: char name[15]; float bpay, h_rent, ma, gpay; public: void get(void) { cout<<"Enter the employee name: "; cin>>name; cout<<"Enter the basic pay: "; cin>>bpay; } void allow(void) { h_rent=bpay*60/100.0; ma=bpay*20/100.0; gpay=bpay+h_rent+ma; } void display(void) { cout<<"Name of the employee: "<<name<<endl; cout<<"Basic Pay : "<<bpay<<endl; cout<<"House Rent : "<<h_rent<<endl; cout<<"Medical allowance: "<<ma<<endl; cout<<"Net Pay: "<<gpay<<endl; } }; int main() { emp data; data.get(); data.allow(); data.display(); return 0; } |
Output:
1 2 3 4 5 6 7 |
Enter the employee name: Khan Enter the basic pay: 50000 Name of the employee: Khan Basic Pay : 50000 House Rent : 30000 Medical allowance: 10000 Net Pay: 90000 |
Example how to use an object in c/c++ programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#include <iostream> using namespace std; class student { private: char name[15]; float sub1, sub2, sub3, total,avg; public: void get_data(void) { cout<<"Enter the student name: "; cin>>name; cout<<"Enter the English marks: "; cin>>sub1; cout<<"Enter the Computer marks: "; cin>>sub2; cout<<"Enter the Maths marks: "; cin>>sub3; total=sub1+sub2+sub3; avg = total/3.0; } void show_data(void) { cout<<"Student name is: "<<name<<endl; cout<<"English marksis: "<<sub1<<endl; cout<<"Computer marks is: "<<sub2<<endl; cout<<"Maths marks is: "<<sub3<<endl; cout<<"Total marks is: "<<total<<endl; cout<<"Average marks is: "<<avg<<endl; } }; int main() { student data; data.get_data(); data.show_data(); return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 |
Enter the student name: khan Enter the English marks: 60 Enter the Computer marks: 80 Enter the Maths marks: 60 Student name is: khan English marksis: 60 Computer marks is: 80 Maths marks is: 60 Total marks is: 200 Average marks is: 66.6667 |
Defining Member Functions Outside the Class:
Member functions of a class can also be defined outside the class. In this case, only the prototype of the member function is declared inside the class.
The member functions are defined outside the class in a similar way as user-defined functions are defined. However, the scope resolution operator ( :: ) is used in the member function declaration to define the function of class outside the class.
The general syntax of a member function definition outside the class is:
Type class_name :: function_name(arguments)
{
Body of the function;
}
Where:
Type
Represents the data type of value returned by the member function.
Class_name
Represents the class name to which the member function belongs.
::
Double colons represent a scope resolution operator that is used to define the body of the member function of a specified class.
Function_name
Represents the name of the member function of a class with arguments.
Body of the function
It is the set of statements of the function.
The member function is defined outside its class only if the body of the function definition in is large. Otherwise the function definition should be defined inside the class.
Example how to call Member Functions Outside the Class in programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#include <iostream> using namespace std; class emp { private: char name[15]; float bpay, h_rent, ma, gpay; public: void get(void); void allow(void); void display(void); }; int main() { emp data; data.get(); data.allow(); data.display(); return 0; } void emp::get(void) { cout<<"Enter the employee name: "; cin>>name; cout<<"Enter the basic pay: "; cin>>bpay; } void emp::allow(void) { h_rent=bpay*60/100.0; ma=bpay*20/100.0; gpay=bpay+h_rent+ma; } void emp:: display(void) { cout<<"Name of the employee: "<<name<<endl; cout<<"Basic Pay : "<<bpay<<endl; cout<<"House Rent : "<<h_rent<<endl; cout<<"Medical allowance: "<<ma<<endl; cout<<"Net Pay: "<<gpay<<endl; } |
Output:
1 2 3 4 5 6 7 |
Enter the employee name: khan Enter the basic pay: 30000 Name of the employee: khan Basic Pay : 30000 House Rent : 18000 Medical allowance: 6000 Net Pay: 54000 |
Storage of Objects in Memory:
When an object of a class is created, space is reserved in the computer memory to hold its data members. Similarly, a separate memory space is reserved for each class object.
The member functions of a class are, however, stored at only one place in the computer memory. All objects of the class use the same member functions to process data.
Therefore, while each object has separate memory space for data members, the member functions of a class are stored in only one place and are stored by all objects of the class.
Example how to use storage of objects in memory in c/c++ programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <iostream> using namespace std; class temp { private: int x; float y; public: void get_data(void) { cout<<"enter x value: "; cin>>x; cout<<"enter y value: "; cin>>y; } void show_data(void) { cout<<"enter x value: "<<x<<endl; cout<<"enter y value: "<<y<<endl; } }; int main() { temp a, b; cout<<"get data in object a:"<<endl; a.get_data(); cout<<"get data in object b:"<<endl; b.get_data(); cout<<"data in object a:"<<endl; a.show_data(); cout<<"data in object b:"<<endl; b.show_data(); return 0; } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 |
get data in object a: enter x value: 4 enter y value: 5 get data in object b: enter x value: 6 enter y value: 7 data in object a: enter x value: 4 enter y value: 5 data in object b: enter x value: 6 enter y value: 7 |
the storage of object in c/c++ a and b as mentioned in the above program example is shown below. These objects use the same member functions.