How do structures in C and C++ differ?
C structure member functions are not permitted but in C++ member functions are permitted.
What is a class? How does it accomplish data hiding?
A class is a way to bind the data and its associated functions together. In class we can declare a data as private for which the functions outside the class can not access the data and thus if accomplish data hiding.
How does a C++ structure differ from a C++ class?
Initially (in C) a structure was used to bundle different of data types together to perform a particular functionality C++ extended the structure to contain functions also. The difference is that all declarations inside a structure are default public.
What are objects? How are they created?
Object is a member of class. Let us consider a simple example. int a; here a is a variable of int type. Again consider class fruit.
{
}
here fruit is the class-name. We can create an object as follows:
fruit mango;
here mango is a object.
How is a member function of a class defined?
member function of a class can be defined in two places:
* Outside the class definition.
* Inside the class definition.
Inside the class definition : same as other normal function.
Outside the class definition : general form:
return-type class-name : function-name (argument list)
{
function body
}
Can we use the same function name for a member function of a class and an outside function in the same program file? If yes, how are they distinguished? If no, give reasons.
Yes, We can distinguish them during calling to main ( ) function. The following example illustrates this:
#include<iostream> using namespace std; void f() { cout<<"Outside Of class \n"; } class santo { public: void f() { cout<<"Inside of class \n"; } }; void main() { f(); // outside f() is calling. santo robin; robin.f(); // Inside f() is calling. }
Describe the mechanism of accessing data members and member functions in the following cases:
- (a) Inside the main program.
- (b) Inside a member function of the same class.
- (c) Inside a member function of another class.
- (a) Using object and dot membership operator.
- (b) Just like accessing a local variable of a function.
- (c) Using object and dot membership operator.
The following example explains how to access data members and member functions inside a member function of another class.
#include<iostream> using name space std; class a { public: int x; void display() { cout<<"This is class a \n"; x=111; } }; class b { public: void display() { a s; cout<<" Now member function 'display()' of class a is calling from class b \n"; s.display(); cout<<" x = "<<s.x<<"\n"; } }; void main() { b billal; // billal is a object of class b. billal.display(); }
When do we declare a member of a class static?
When we need a new context of a variable the n we declare this variable as static.
What is a friend function? What are the merits and demerits of using friend functions?
A function that acts as a bridge among different classes, then it is called friend function.
Merits :
We can access the other class members in our class if we use friend keyword. We can access the members without inheriting the class.
demerits :
Maximum size of the memory will occupied by objects according to the size of friend members.
State whether the following statements are TRUE or FALSE.
- (a) Data items in a class must always be private.
- (b) A function designed as private is accessible only to member functions of that class.
- (c) A function designed as public can be accessed like any other ordinary functions.
- (d) Member functions defined inside a class specifier become inline functions by default.
- (e) Classes can bring together all aspects of an entity in one place.
- (f) Class members are public by default.
- (g) Friend junctions have access to only public members of a class.
- (h) An entire class can be made a friend of another class.
- (i) Functions cannot return class objects.
- (j) Data members can be initialized inside class specifier.
- (a) FALSE
- (b) TRUE
- (c) FALSE
*A function designed as public can be accessed like any other ordinary functions from the member function of same class.
- (d) TRUE
- (e) TRUE
- (f) FALSE
- (g) FALSE
- (h) TRUE
- (i) FALSE
- (j) FALSE
Next Previous