C++ Class
A class is the collection of related data and function under a single name. A C++ program can have any number of classes. When related data and functions are kept under a class, it helps to visualize the complex problem efficiently and effectively.A Class is a blueprint for objects
When a class is defined, no memory is allocated. You can imagine like a datatype.
int var;
The above code specifies var is a variable of type integer; int is used for specifying variable var is of integer type. Similarly, class are also just the specification for objects and object bears the property of that class.Defining the Class in C++
Class is defined in C++ programming using keyword class followed by identifier(name of class). Body of class is defined inside curly brackets an terminated by semicolon at the end in similar way as structure.class class_name
{
// some data
// some functions
};
Example of Class in C++
class temp
{
private:
int data1;
float data2;
public:
void func1()
{
data1=2;
}
float func2()
{
data2=3.5;
retrun data;
}
};
Explanation for above program:As mentioned, definition of class starts with keyword class followed by name of class(temp) in this case. The body of that class is inside the curly brackets and terminated by semicolon at the end. There are two keywords: private and public mentioned inside the body of class.
Keywords: private and public
Keyword private makes data and functions private and keyword public makes data and functions public. Private data and functions are accessible inside that class only whereas, public data and functions are accessible both inside and outside the class. This feature in OOP is known as data hiding. If programmer mistakenly tries to access private data outside the class, compiler shows error which prevents the misuse of data. Generally, data are private and functions are public.C++ Objects
When class is defined, only specification for the object is defined. Object has same relationship to class as variable has with the data type. Objects can be defined in similary way as structure is defined.Syntax to Define Object in C++
class_name variable name;
For the above defined class temp, objects for that class can be defined as:temp obj1,obj2;
Here, two objects (obj1 and obj2) of temp class are defined.Data member and Member functions
The data within the class is known as data member. The function defined within the class is known as member function. These two technical terms are frequently used in explaining OOP. In the above class temp, data1 and data2 are data members and func1() and func2() are member functions.Accessing Data Members and Member functions
Data members and member functions can be accessed in similar way the member of structure is accessed using member operator(.). For the class and object defined above, func1() for object obj2 can be called using code:obj2.func1();
Similarly, the data member can be accessed as:object_name.data_member;
Note: You cannot access the data member of the above class temp because both data members are private so it cannot be accessed outside that class.
Illustration for above theory of object and Class in C++
/* Program to illustrate working of Objects and Class in C++ Programming */
#include <iostream>
using namespace std;
class temp
{
private:
int data1;
float data2;
public:
void int_data(int d)
{
data1=d;
cout<<"Number: "<<data1;
}
float float_data()
{
cout<<"\nEnter data: ";
cin>>data2;
return data2;
}
};
int main()
{
temp obj1, obj2;
obj1.int_data(12);
cout<<"You entered "<<obj2.float_data();
return 0;
}
Output: Number: 12
Enter data: 12.43
You entered: 12.43
Explanation of Program
In this program, two data members data1 and data2 and two member functionint_data()
and float_data()
are defined under temp class. Two objects obj1 and obj2 of that class are declared. Function int_data()
for the obj1 is executed using code obj1.int_data(12);
, which sets 12 to the data1 of object obj1. Then, function float_data()
for the object obj2 is executed which takes data from user; stores it in data2 of obj2 and returns it to the calling function.Note: In this program, data2 for object obj1 and data1 for object obj2 is not used and contains garbage value.
Defining Member Function Outside the Class
A large program may contain many member functions. For the clarity of the code, member functions can be defined outside the class. To do so, member function should be declared inside the class(function prototype should be inside the class). Then, the function definition can be defined using scope resolution operator ::. Learn more about defining member function outside the class.
No comments:
Post a Comment