You are on page 1of 21

Que 1: What is the difference between structure in C and C++.

Ans:
 The standard C does not allows the struct data type to be treated like built-in
types. For example:
struct complex
{
float x;
float y;
};
struct complex c1, c2, c3;
The complex number c1, c2 and c3 can be easily be assigned values using the dot
operator, but we cannot add two complex numbers or subtract one from another.
For example
c3 = c1 + c2; is illegal in C.
 C structure does not allow data hiding while C++ structure allows data hiding.
 The keyword struct can be omitted in the declaration of structure variable in C++
For example: student A;
While it is illegal in C. we have to write like this
struct student A
Que 2: What is the difference between structure and class in C++.
Ans:
 By default the member of structure are private while by default the member of
structure are public.
Que 3: What is Class? How to specifying a class?
Ans:
 A class is a way to bind data and its associated functions together. This concept is
known as data encapsulation.
 Class allows data to be hidden from external use.
 When we declare a class we are creating new abstract data type using which we
can create new variables of its type which is known as objects.
 Class specification has two parts:
(1) Class declaration

(2) Member function definition


 The general syntax of class declaration is as follow:
Class classname
{
Private:
Variable declaration;
Function declaration;
Public:
Variable declaration;
Function declaration;
};
 The body of the class is enclosed between curly brackets and followed by
semicolon. The body contains declaration of variable as well as function which are
known as member of the class.
 The variables declared inside class are known as data member and function are
known as member function.
 Basically there are two section in class named public and private. They are known
as visibility mode.
 The members which are declared under private section can be accessed only within
the class. We can not access them directly using the object of the class.
 The members which are declared under public section can be accessed from
outside the class. We can access them directly using the object of the class.
 When we do not specify any visibility mode in the class while declaring data
members they become private by default.
 Example:
Class test
{
Int a, b;
Public:
Void getdata();
{
Cout<<”Enter Value of a and b”;
Cin>>a>>b;
}
Void putdata()
{
Cout<<”A=”<<a<<endl<<”B=”<<b;
}
};
Int main()
{
Test t1;
T1.getdata();
T1.putdata();
}
Que 4: How to create object?
Ans:
 There are two methods for creating objects:

(1) We can create object while declaring the class. When we defined the class we
can write the name of the object immediately after the closing bracket for
example:
Class test
{
Int a,b;
Public:
Void getdata();
Void putdata();
}t1,t2,t3;

(2) We can create object separately inside the main function. The general syntax
for creating object inside main function is as below:
Classname objectname;
For example:
Test t1,t2,t3;
Que 5: How to access class members?
Ans:
 Basically class members are classified as public or private.
 The private data members can not be accessed using the name of the object. We
can access them using public member function. While public members can be
accessed using the name of the object.
 The general syntax for accessing public member using object is as follow:
Objectname.datamember = value;
Objectname.memberfunction(argumentlist);
 Consider the following example:
Class test
{
Int b;
Public:
Int a;
Void setb()
{
B=20;
}
};
Int main()
{
Test t1;
T1.a=10; works OK
T1.b=20; error
T1.setb(); works OK
Return 0;
}
 Because b is private member we can not access it using name of the object. So we
create a public member function setb() to access b.
 A is public member so we can access it directly using object name.
Que 6: How to define member functions?
Ans:
 We can define member function of the class in two different way:
(1) inside class
(2) outside class
 Inside Class:
 When we declare the function in the class at the same time we can also give the
definition of the function in the class as shown below:
Class test
{
Int a,b;
Public:
Void getdata()
{
Cout<<”Enter Value of a”;
Cin>>a>>b;
}
};
The function defined inside the class becomes inline bydefault.
 Outside Class:
 We can also define the member function outside the class. But at that time we
have to use membership label to tell compiler this function belongs to which class
using scope resolution operator as follow:
Returntype classname::functionname(parameter list)
{
Function definition
}
 For example:
Class test
{
Int a,b;
Public:
Void getdata();
};
Void test :: getdata()
{
Cout<<”Enter Value of a”;
Cin>>a>>b;
}
Que 7: How to make an outside function Inline?
Ans:
 The function defined inside the class are becomes inline by default so all the
restriction that applied to inline function are also applied to the member function
defined inside the class.
 However we can also make the function inline which is defined outside the class.
 To make the outside function inline we have to just precede the definition with the
keyword inline.
 Following example shows how to make outside function inline.
Class test
{
Int a,b;
Public:
Void getdata()
{
Cout<<”Enter value of a and b”;
Cin>>a>>b;
}
Void putdata();
};
Inline void test::putdata()
{
Cout<<”A=”<<a<<endl<<”B=”<<b;
}
Que 8: Explain Nesting of member function.
Ans:
 A member function can be called from inside another member function of the same
class. It is known as nesting of member function.
 Following example shows the use of nesting member function:
Class test
{
Int m,n;
Public:
Void getdata();
Void putdata();
int largest();
};
int test::largest()
{
If(m>n)
Return m;
Else
Return n;
}
Void test::getdata()
{
Cout<<”Enetr value of m and n”;
Cin>>m>>n;
}
Void test::putdata()
{
Cout<<”largest=”<<largets();
}
Int main()
{
Test t1;
T1.getdat();
T1.putdata();
Return 0;
}

 In the above example we have declared three member function getdata(),


putdata() and largets().
 But we have called only two member functions getdata() and putdata() using the
name of the object inside main function.
 We have called the third member function largest() from inside the putdata()
member function. This concept is known as nested member function.
Que 9: Explain Private member function.
Ans:

 Sometimes it is necessary to hide member function from outside the class at that
time we have to declare that function as private.
 Since private member function is not accessible using the object and outside the
class we have to access it from the public member function of the same class.
 Consider the following example:
Class test
{
Int m,n;
int largest();
Public:
Void getdata();
Void putdata();
};
int test::largest()
{
If(m>n)
Return m;
Else
Return n;
}
Void test::getdata()
{
Cout<<”Enetr value of m and n”;
Cin>>m>>n;
}
Void test::putdata()
{
Cout<<”largest=”<<largets();
}
Int main()
{
Test t1;
T1.getdat();
T1.putdata();
Return 0;
}

 In the above example the member function largest() is declared as private so we


have to access it from the public member function.
Que 10: Explain Memory allocation for objects.
Ans:

 The memory space for the object is allocated when they are declared and not when
the class is specified. But this statement is partly true.
 Actually the class consists of member function and data members. The member
functions are common for all the objects so the memory space for the member
function is allocated once when they are defined. But the value for the data
member of different objects is different so the memory space for the data member
is allocated separately for each object when they are declared.
 All the data members of each object will have separate memory space

Object 1 Object 2 Object 3

Data Member 1 Data Member 1 Data Member 1

Data Member 2 Data Member 2 Data Member 2

All the member Member function 1


functions will
share same Member function 2
memory space
for all the Member function 3
objects.

Que 11: Explain static data member with example.


Ans:

 We can declare the data members of a class as static. In C++ static member
variable has some special characteristics, which are as follows:
• A member of a class is declared as static and when an object of that class is
created, the member variable will be initialized to zero (0). No other initialization
is permitted.
• Only one copy of a static data member is created for the entire class and is shared
by all the objects of that class.
• It is visible only within a class but its lifetime is the entire program.
 Static variables are normally used to maintain values common to the entire class.
Note that static member declaration is in class but a static member must be
defined outside the class using scope resolution operator (::) because the static
data members are stored separately rather than as a part of an object. It can be
accessed separately without use of any object.
 Example:
#include<iostream.h>
#include<conio.h>
class item
{
static int count;
int number;
public:
void getdata(int a)
{
number = a;
count++;
}
void getcount()
{
cout<<”count:”;
count<<count<<”\n”;
}
};
int item::count;
int main()
{
item a,b,c;
a.getcount();
b.getcount();
c.getcount();
a.getdata(100);
b.getdata(200);
c.getdata(300);
a.getcount();
b.getcount();
c.getcount();
return 0;
}
 Output:
Count: 0
Count: 0
Count: 0
Count: 3
Count: 3
Count: 3

Que 12: Explain static member function with example


Ans:
 Static member functions

 We can also declare member functions as static like data members. To declare a
member function as static you have to consider following properties of static
member function.
• A static member function can access only other static data members or static
member functions declared in the same class.
• A static member function can be called using the class name not the object name
of that class.
class-name::function-name;
 Example:
#include <iostream.h>
#include <conio.h>
class test
{
private:
int code
static int count;
public:
void setcode(void);
void showcode(void);
static void showcount(void);
};
int test :: count;
void test :: setcode(void)
{
code = ++count;
}
void test :: showcode(void)
{
cout<<"Object number is "<< code<< endl;
}

void test :: showcount(void)


{
cout<<"Count:"<< count << endl;
}
int test::count;
int main(void)
{
test t1, t2;
clrscr();
t1.setcode();
t2.setcode();
test :: showcount();
test t3;
t3.setcode();
test :: showcount();
cout<< endl;
c1.showcode();
c2.showcode();
c3.showcode();
getch();
return(0);
}
Output:
Count: 2
Count: 3
Object number is 1
Object number is 2
Object number is 3
Que 13: Explain array of objects.
Ans:

 As we can declare an array of any built in type we can also declare an array
of variables of type class. This array of variables called as an array of objects.
For example, consider the earlier employee class,
class employee
{
int emp_id;
char emp_name[20];
float salary;
public:
void getdata(int id, char name[20], float sal);
void putdata(void);
};
Here the identifier employee is user defined data type and we can create an
array
of objects of type employee as follows:
employee e[4];
Here we have created four objects e[0], e[1], e[2] and e[3] of type employee.
Now if you want to access any member function through object as given below,
e[0].putdata();
Now let’s see a program of an array of objects.
#include <iostream.h>
#include <conio.h>
#include <string.h>
class employee
{
private:
int emp_id;
char emp_name[20];
float salary;
public:
void getdata(void);
void putdata(void);
};
void employee :: getdata(void)
{
cout<<"Enter employee id ";
cin>> emp_id;
cout<<"Enter employee name ";
cin>> emp_name;
cout<<"Enter employee salary ";
cin>> salary;
}
void employee :: putdata(void)
{
cout<<"Employee id is: "<< emp_id << endl;
cout<<"Employee name is: "<< emp_name << endl;
cout<<"Employee salary is: "<< salary << endl;
}
int main(void)
{
employee e[3];
clrscr();
cout<<" Input data of three employees" << endl;
for(int i=0;i<3; ++i)
{
e[i].getdata();
cout<< endl;
}
cout<<" Output of three employees" << endl;
for(i=0;i<3; ++i)
{
e[i].putdata();
cout<<endl;
}
getch();
return(0);
}

Output:

Input data of three employees


Enter employee id 11
Enter employee name John
Enter employee salary 5000

Enter employee id 12
Enter employee name Adams
Enter employee salary 7000

Enter employee id 13
Enter employee name Jack
Enter employee salary 10000

Output of three employees


Employee id is: 11
Employee name is: John
Employee salary is: 5000

Employee id is: 12
Employee name is: Adams
Employee salary is: 7000

Employee id is: 13
Employee name is: Jack
Employee salary is: 10000

Que 14: Explain how to pass object as function argument and return object in
detail.
Ans:
 We can pass object as a function argument in to way:

(3) A copy of the object is passed to the function which is known as call by
value

(4) An address of the object is passed to the function which is known as call
by reference.
 Let’s see an example of passing objects as argument and returning objects
from the function.
#include <iostream.h>
#include <conio.h>
class distance
{
private:
int feet;
float inches;
public:
void get_distance(int, float);
distance add_distance(distance);
void put_distance(void);
};
void distance :: get_distance(int f, float i)
{
feet = f;
inches = i;
}
distance distance :: add_distance(distance d2)
{
distance d;
d.feet = feet + d2.feet; // feet is data of object d1.
d.inches = inches + d2.inches; // inches is data of object d1.
if(d.inches >= 12.0)
{
d.inches -= 12.0;
d.feet++;
}
return d;
}
void distance :: put_distance(void)
{
cout<<"Feet "<< feet <<" -inches " << inches << endl;
}
int main(void)
{
distance d1,d2,d3;
clrscr();
d1.get_distance(11, 9.5);
d2.get_distance(5, 4.5);
d3 = d1.add_distance(d2);
d1.put_distance();
d2.put_distance();
d3.put_distance();
getch();
return(0);
}
Output:
Feet 11 – inches 9.5
Feet 5 – inches 4.5
Feet 17 – inches 2

 We have declared a member functions add_distance() with an argument


of object of type distance and that function returns an object of type
distance.
 In main () function we create three objects d1, d2 and d3 of type
distance. Then we call get_distance() with arguments through d1 and
d2 objects. Then we call add_distance() function through d1 object with
argument d2 and that function returns an object of type distance, that is
stored in object d3.

 When the function add_distance() is called, in that function we are adding


two data members feet of objects d1 and d2 and two data members
inches of objects d1 and d2 into another data members feet and inches
of object d. Note that this function we have called through d1 object so
there is no need of specifying object name (d1) in prefix of feet and inches
data members. We have passed d2 object so to access the members of d2
we must write object name (d2) in front of data members. The addition of
data members of d1 and d2 objects is stored in the data members of object
d. And then that function returns object d that is assigned to object d3.
Then in main() function we call put_distance() function through all three
objects d1, d2 and d3 to print the data members of all three objects.
Que 15: Explain friend function with example.
Ans:
 In the real life the friend member means a member who is close to you but
not your family member. In same way we can also define a function as friend in
the class, which will not be the member of that class but that function can
access the members of the class.
 Up to now we have seen that a non-member function cannot access the
private data members of that class but in some situation we need to give
access of the private data member to the outside function of class.

 For this situation C++ provides the feature of friend function. A friend
function can be friend of more than one class. To define a member function of a
class as friend through writing a keyword friend in front of function name at
the time of declaration. The general form of friend function is as follows:
class <class name>
{
private:
……
……
public:
……
friend <return type> <function name> (arguments);
};

 After a function is declared in the class that function must be defined


outside or inside the class, at the time of defining a friend function, no need of
using keyword friend or scope resolution operator (::). A friend function can
be declared in any number of classes. It has certain special characteristics,
which are as follows:
(1) It is not in the scope of the class to which it has been declared as friend.
(2) Since it is not in the scope of the class, it cannot be called using the object
of that class. It can be invoked like a normal function without the help of any
object.
(3) Unlike member function, it cannot access the member names directly and
has to use an object name and dot membership operator with each member
name.
(4) It can be declared as private or public in the class without affecting its
meaning.
(5) Usually, it has the objects as arguments.
 We can also declare all the member functions as friend of another class then that
class will be called as friend class. The friend class can be declared as follows:
class a
{
private:
……
……
public:
……
friend class b;
};

 Here all the member functions of class b are friend of class a. A member
function of one class can be friend function of more than one class, in such
case a member function should be defined using the scope resolution
operator (::) in another classes. For example,
class a
{
private:
……
……
public:
……
void abc();
};
class b
{
private:
……
……

public:
……
friend void a :: abc(); // abc() is friend of class b.
};
 A function abc() is declared in class a and if you want to declare or use that
function in class b then declare that function as friend in class b using scope
resolution operator (::) as shown in the given example. Now let’s see a
program of friend function.
Que 16: what is local class?
Ans:

 Classes can be defined inside a function or a block. Such classes are known
as local classes.
 For example:
Void test(int a)
{
……………
……………
Class student
{
………………..
………………..
};
………………..
Student s1(a);
}
 The local classes can use global variables and static members defined inside
the function.
 The function can not access private member of the class. If you want to
access the private member you have to make that function friend.
 They cannot have static data members and member function must be
defined inside the class.

You might also like