Computer Science - FAQ
Computer Science - FAQ
OOPS
Emphasis on objects
Follows bottom-up
approach in program
design
Its data hiding features
prevent accidental
change in data
Features like
encapsulation,
polymorphism, etc., are
present
2. Define:
i) Data hiding
The data members declared under the private section which
are NOT available outside the class is known as data hiding.
ii) Data encapsulation
The method of binding the data members and member
functions into a single unit is called data encapsulation.
iii) Data abstraction
The method of representing the essential details and
thereby hiding the background details is known as data
abstraction.
iv) Modularity
A property of the system that has been decomposed into a
set of cohesive and loosely coupled modules is known as
modularity.
v) Class
class type. The static data members are usually maintained to store
values common to the entire class.
Example:
class abc
{
int a;
static int b;
};
int abc::b=3;
8. What are static member functions?
A) Member functions that access only the static members of the class. They
can be declared using the keyword static before the function definition in
the class program.
Example:
class abc
{
int a;
static int b;
public:
void indata()
{
cin>>a;
}
static void show()
{
cout<<b;
}
};
int abc::b=3;
void main()
{
abc ob;
ob.indata();
abc::show();
}
9. What is a nested class? Give example.
A) A class declared within another class is called a nested class.
Example:
class outer
{
int a;
class inner
{
int b;
public:
int c;
void prn()
{
cout<<Inner<<endl;
cin>>b>>c;
}
};
inner ob1;
public:
inner ob2;
void second()
{
cout<<Outer<<endl;
cin>>a;
}
};
10. Differentiate between structure and class.
A)
STRUCTURE
CLASS
It is a collection of data
types.
It is a collection of data
types and member
functions.
They are saved as
private by default.
Example:
class example
{
int x;
float y;
char z;
public:
example()
{
x = 17;
y = 99.797;
z = f;
} };
17. What are the types of constructors? Give an example.
A) There are three types of constructors:
Default constructor:
The constructor that will not take any arguments is called default
constructor.
Parameterized constructor:
The constructor which takes arguments is called parameterized
constructor.
Copy constructor:
It is used to copy one object into another object of the same class.
Example:
class demo
{
int x;
float y;
char z;
public:
demo()
//DEFAULT CONSTRUCTOR
{
x = 5;
y = 20.7;
z = u;
}
demo(int a, float b, char c)
//PARAMETERIZED
{
x = a;
y = b;
z = c;
}
demo(demo &t)
//COPY CONSTRUCTOR
{
x = t.x;
y = t.y;
z = t.z;
}
void disp()
//MEMBER FUNCTION
{
cout<<x<< : <<y<< : <<z<<endl;
} };
void main()
{
demo ob;
//DEFAULT CONSTRUCTOR
ob.disp();
demo ob1(5, 20.7, u);
//PARA - IMPLICIT
ob1.disp();
demo ob2 = demo(5, 20.7, u);//PARA. -EXPLICIT
ob2.disp;
demo ob3(ob2);
//COPY METHOD 1
ob3.disp;
demo ob4 = ob3;
//COPY METHOD 2
ob4.disp(); }
18. Differentiate between implicit and explicit constructors.
A)
Implicit:
In this method, the constructor is invoked even when its name has not
been mentioned in the statement.
Explicit:
By explicit call, the name of the constructor is explicitly provided to
invoke it so that the object can be initialized.
19. What are temporary instances? Give examples.
A) Temporary instances live in the memory for as long as they are being
used or referenced in an expression and after that they die. Temporary
instances are anonymous i.e., they do not bear a name.
Example:
class sample
{
int i, j;
public:
sample(int a, int b);
{
i = a;
j = b;
}
void print()
{
cout<<i<< : <<j<<endl;
}
};
void main()
{
sample s(8, 10);
s.print();
sample(6,4).print();
}
//TEMPORARY INSTANCE
y = 20.7;
z = u;
}
demo(int a, float b, char c)
{
x = a;
y = b;
z = c;
}
void disp()
{
cout<<x<< : <<y<< : <<z<<endl;
} };
void main()
{
demo ob;
ob.disp();
demo ob1(5, 20.7, u);
ob1.disp();
}
23. Explain inheritance.
A) Inheritance is the method or extracting some or all the properties of one
class(base class) into another class(derived class)
24. What is the need for inheritance?
Capability to express the inheritance relationship which ensures
the closeness with the real world models.
Reusability of code
It is faster to develop.
Easy maintenance.
Easy to extend.
Transitive nature of inheritance.
Multilevel inheritance:
When a derived class inherits from a class that itself inherits from
another class, it is known as multilevel inheritance.
Y
Z
Multiple inheritance:
When a derived class inherits from multiple base classes, it is called
multiple inheritance.
X
Z
Hierarchial inheritance:
When many derived classes inherit from a single base class, it is known
as hierarchial inheritance.
X
W
Hybrid inheritance:
When a derived class inherits from multiple base classes and all of its
base classes inherit from a single base classes inherit from a single base
class then this form of inheritance is known as hybrid inheritance.
26. Explain function overloading with example.
A) A function name having several definitions that are differentiable by the
number or type of arguments is known as function overloading.
Example:
void area(int r)
{
cout<<"Area of Circle: "<<pi*r*r;
}
void area(int a, int b)
{
cout<<"Area of rectangle: "<<a*b;
}
void area(int a,int b, int c)
{
float ar, s;
s = (a+b+c)/2;
ar = sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"Area of triangle: "<<ar;
}
void main()
{
int ch;
int a,b,r,c;
cout<<"1.Area of Circle<<endl;
cout<<2.Area of Rectangle<<endl;
cout<<3.Area of Triangle<<endl;
cout<<4.Exit<<endl;
cout<<Enter your Choice: ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter Radius of the Circle:";
cin>>r;
area(r);
break;
case 2:
cout<<"Enter Sides of the Rectangle:";
cin>>a>>b;
area(a,b);
break;
case 3:
cout<<"Enter sides of the Triangle:";
cin>>a>>b>>c;
area(a,b,c);
break;
case 4:
break;
}
getch();
}
27. What is polymorphism?
A) It is a property by which the same message can be sent to objects of
several different classes and each object can respond to it in a different
way depending upon its class. In C++, polymorphism is implemented
through overloading.
28. Explain function signature with an example.