Q1: What Is Inheritance? Discuss Its Types
Q1: What Is Inheritance? Discuss Its Types
Inheritance can be defined as the process where one class acquires the properties
(methods and fields) of another. The class which inherits the properties of other is
known as subclass (derived class, child class) and the class whose properties are
inherited is known as super class (base class, parent class).
A class derivation list names one or more base classes and has the form:
class derived-class: access-specifier base-class
Where access-specifier is one of public, protected, or private, and base-class is
the name of a previously defined class. If the access-specifier is not used, then it is
private by default.
Multiple Inheritances
In this type of inheritance a single derived class may inherit from two or more than
two base classes.
Hierarchical Inheritance
In this type of inheritance, multiple derived classes inherits from a single base
class.
Multilevel Inheritance
In this type of inheritance the derived class inherits from a class, which in turn
inherits from some other class. The Super class for one, is sub class for the other.
The main features of object oriented programming which you will be using in C++.
1.
Objects
2.
Classes
3.
Abstraction
4.
Encapsulation
5.
Inheritance
6.
Overloading
7.
Exception Handling
Objects
Objects are the basic unit of OOP. They are instances of class, which have data members and
uses various member functions to perform tasks.
Class
It is similar to structures in C language. Class can also be defined as user defined data type but
it also contains functions in it. So, class is basically a blueprint for object. It declare & defines
what data variables the object will have and what operations can be performed on the class's
object.
Abstraction
Abstraction refers to showing only the essential features of the application and hiding the
details. In C++, classes provide methods to the outside world to access & use the data
variables, but the variables are hidden from direct access.
Encapsulation
It can also be said data binding. Encapsulation is all about binding the data variables and
functions together in class.
Inheritance
Inheritance is a way to reuse once written code again and again. The class which is inherited is
called base calls & the class which inherits is called derived class. So when, a derived class
inherits a base class, the derived class can use all the functions which are defined in base class,
hence making code reusable.
Polymorphism
Polymorphism makes the code more readable. It is a features, which lets is create functions
with same name but different arguments, which will perform differently. That is function with
same name, functioning in different
Overloading
Overloading is a part of polymorphism. Where a function or operator is made & defined many
times, to perform different functions they are said to be overloaded.
Exception Handling
Exception handling is a feature of OOP, to handle unresolved exceptions or errors produced at
runtime.
Types of Constructor:
The various types of Constructor are as follows:-
2.
3.
1.
struct student
{
int a;
char b;
float c;
}s1;
4. The total memory required to store a structure
variable is equal to the sum of size of all the
members. In above case 7 bytes (2+1+4) will
be required to store structure variable s1.
4.
union student
{
int a;
char b;
float c;
}s1;
In above example variable marks are of float
type and have largest size (4 bytes). So the
is 4 bytes.
5.
6.
a
b
c
5. It Consume less Memory
abc
{
Private:
int a;
public:
void m()
{
cout<<"Enter two values:";
cin>>a;
}
friend void ff(abc obj);
};
void ff(base obj)
{
Cout<<obj.a;
}
void main()
{
clrscr();
abc obj1l
obj1.m();
abc(obj1);
getch();
}
b) Inline Function
If a function is inline, the compiler places a copy of the code of that function at
each point where the function is called at compile time. Any change to an inline
function could require all parts of the function to be recompiled because compiler
would need to replace all the code once again otherwise it will continue with old
functionality. We have to use inline keyword to declare it.
#include <iostreaml.h>
#include<conio.h>
{
cout<<"hello";
}
void main()
{
Void hello();
hello();
File Handling concept in C++ language is used for store a data permanently in computer. Using file handling we can
store our data in Secondary memory (Hard disk).We use file handing for permanet storage and The transfer of input data or output - data from one computer to another can be easily done by using files.
For read and write from a file you need another standard C++ library called fstream, which defines three new data
types:
1. Ofstream: This is used to create a file and write data on files
2. ifstream: This is used to read data from files
3. fstream: This is used to both read and write data from/to files
File Opening mode
Mode
Meaning
Purpose
ios :: out
Write
Open the file for write only.
ios :: in
read
Open the file for read only.
ios :: app
Appending
Open the file for appending data to end-of-file.
ios :: ate
Appending
take us to the end of the file when it is opened.
Functions use in File Handling
Function
Operation
open()
To create a file
close()
To close an existing file
get()
Read a single character from a file
put()
write a single character in file.
read()
Read data from file
write()
Write data into file.
Polymorphism is the ability of an object to take on many forms. The most common
use of polymorphism in OOP occurs when a parent class reference is used to refer
to a child class object.
Virtual Function: A virtual function is a member function that is declared within a
base class and redefined by a derived class. To create virtual function, precede the
functions declaration in the base class with the keyword virtual. When a class
containing virtual function is inherited, the derived class redefines the virtual
function to suit its own needs.
if we want to call derived class function using base class pointer, it can be achieved
by defining the function as virtual in base class.
Early Binding: which function is going to be called by object is decided by
compiler at compile time known as early binding.
Late Binding: which function is going to be called by object is decided at run time
using pointer object known as late binding. It is done by using pointer.
class Base
{
public:
virtual void show()
{
cout << "Base class";
}
};
class Derived:public Base
{
public:
void show()
{
cout << "Derived Class";
}
}
void main()
{
Base* b;
Derived d;
b = &d;
b->show();
}
Q8: What is function? Discuss call by value and call by reference.
int a = 100;
int b = 200;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
swap(a, b);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;
}
void swap(int &x, int &y)
{
int temp;
temp = x;
x = y;
y = temp;
cout<<x<<y;
}
Q9: Explain Conditional statements in details
The statements in the programs presented above have all been sequential, executed in the order they appear in the main
program.
Description
if statement
if...else statement
switch statement
nested if statements
nested
statements
switch
Loop Type
while loop
for loop
do...while loop
nested loops
Description
Repeats a statement or group of statements while a given condition is true.
It tests the condition before executing the loop body.
Execute a sequence of statements multiple times and abbreviates the code
that manages the loop variable.
Like a while statement, except that it tests the condition at the end of the
loop body
You can use one or more loop inside any another while, for or do..while
loop.