Professional Documents
Culture Documents
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
(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;
}
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;
}
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
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
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;
}
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:
Enter employee id 12
Enter employee name Adams
Enter employee salary 7000
Enter employee id 13
Enter employee name Jack
Enter employee salary 10000
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
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);
};
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.