Data Abstraction, Data Hiding C++ concepts
Introduction to Class in C++ language

Q. 1 What is data abstraction?
The insulation of data from direct access by the program is called as data hiding or information binding. The data is not accessible to the outside world and only those functions, which are wrapped in the class, can access it.


Q. 2 What are data members and member functions?
Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight, and cost and uses functions to operate on these attributes. The attributes are sometimes called as data members because they hold information. The functions that operate on these data are called as methods or member functions.
For example: 
int a,b; // a,b are data members of type int
float c; // c are data members of type float
void getdata ( ) ; // member function 
int add(int a, int b); // member function 

Q. 3 How to declare a class in C++ language.
A class is very powerful feature of OOP's. It is the logical way, to group together data members and member functions. Thus a class represents a set of individual objects. We can create more then one objects of the classes. No memory is allocated when a class is created. Memory is allocated only when an object is created.
Class declaration example:
class add
{
// data members of class add
int a, b;
int x, y;
public:
// member functions of class add
void getdata(int x, int y);
void add(int a, int b);
};

Q. 4 How the member functions are defined ? 
Member functions can be defined in two ways which are given below:
1. Inside the class definition: This method of defining member function is to replace the function declaration by the actual function definition inside the class. It is treated as inline function. 
For example:
class add
int a,b ; 
void getdata(int x,int y) 
a=x; 
b=y; 
}; 
2. Outside the class definition: Member function can be defined by using scope resolution operator (::) 
For example:
class item 
int a,b ; 
void getdata(int x,int y);
}; 
void add :: getdata(int x,int y);
a=x; 
b=y; 
}

Q. 5 How to create an object of a class? 
Once the class has been declared, we can create variables of that type by using the class_name 
For example:
class_name x; //memory for x is created 
add obj; // here obj is an object of class add.

Q. 6 How to access a class member of a class? 

Class member like ( data members and member functions) are access with the help of class object and dot (.) operator.
Syntax:
object-name.function-name(actual arguments) 
For example:
x.getdata(100,75.5); 
x.add(2, 4);


Thanks
Mukesh Rajput

Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: