answers c++
answers c++
#include<iostream.h>
#include<conio.h>
void main()
{
int a=2;
try
{
if(a==1)
throw a; //throwing integer ex
ception
else if(a==2)
throw 'A'; //throwing character
exception
else if(a==3)
throw 4.5; //throwing float exce
ption
}
catch(int a)
{
cout<<"\nInteger exception caught.";
}
catch(char ch)
{
cout<<"\nCharacter exception caught.";
}
catch(double d)
{
cout<<"\nDouble exception caught.";
}
cout<<"\nEnd of program.";
}
Output :
class exe
{
double a,b;
public:
void read()
{
cout<<"\nEnter two double type numbers:";
cin>>a>>b;
}
void div()
{
try{
if(cin.fail())
throw "Bad input!";
if( b == 0 )
throw 0;
cout<<"\nAns is "<<a/b;
}
catch(const int n)
{
cout << "\nDivision by " << n << " not
allowed\n";
}
catch(const char* Str)
{
cout<< Str;
}
}
};
int main()
{
exe ex;
ex.read();
ex.div();
return 0;
}
5th
1. Explain Inheritance. List and explain with neat
diagrams, the different
types of Inheritances in C++ programming.
Inheritance is the capability of one class to acquire
properties and characteristics from another class. The
class whose properties are inherited by other class is
called the Parent or Base or Super class. And, the class
which inherits properties of other class is
called Child or Derived or Sub class.
Inheritance makes the code reusable. When we inherit
an existing class, all its methods and fields become
available in the new class, hence code is reused.
In C++, we have 5 different types of Inheritance.
Namely,
1. Single Inheritance
2. Multiple Inheritance
3. Hierarchical Inheritance
4. Multilevel Inheritance
5. Hybrid Inheritance (also known as Virtual
Inheritance)
Single Inheritance in C++
In this type of inheritance one derived class inherits
from only one base class. It is the most simplest form of
Inheritance.
Multiple Inheritance in C++
In this type of inheritance a single derived class may
inherit from two or more than two base classes.
#include <iostream>
class A {
public:
void show()
};
class B : public A {
};
class C : public A {
};
};
int main()
D object;
object.show();
Multilevel Inheritance
In C++ programming, not only you can derive a class
from the base class but you can also derive a class from
the derived class. This form of inheritance is known as
multilevel inheritance.
class A {
... .. ...
};
class B: public A {
... .. ...
};
class C: public B {
... ... ...
};
#include <iostream>
using namespace std;
class A {
public:
void display() {
cout<<"Base class content.";
}
};
int main() {
C obj;
obj.display();
return 0;
}
Output
Base class content.
In this program, class C is derived from class B (which
is derived from base class A).
The obj object of class C is defined in
the main() function.
When the display() function is called, display() in
class A is executed. It's because there is
no display() function in class C and class B.
The compiler first looks for the display() function in
class C. Since the function doesn't exist there, it looks
for the function in class B (as C is derived from B).
The function also doesn't exist in class B, so the
compiler looks for it in class A (as B is derived
from A).
If display() function exists in C, the compiler
overrides display() of class A (because of member
function overriding).
th
6
Define Pointer and this Pointer. What does this
pointer point to?
C++ pointers are easy and fun to learn. Some C++ tasks
are performed more easily with pointers, and other C++
tasks, such as dynamic memory allocation, cannot be
performed without them.
As you know every variable is a memory location and
every memory location has its address defined which
can be accessed using ampersand (&) operator which
denotes an address in memory
This pointer:
Every object in C++ has access to its own address
through an important pointer called this pointer.
The this pointer is an implicit parameter to all member
functions. Therefore, inside a member function, this
may be used to refer to the invoking object.
Friend functions do not have a this pointer, because
friends are not members of a class. Only member
functions have a this pointer.
changed.
Explain Virtual Functions and List the rules for virtual
functions.
C++ virtual function
o A C++ virtual function is a member function in the
}
double get_x(){return x;}
double get_y(){return y;}
virtual void display_area(){}
};
}
};
class rectangle:public shape
{
public:
void display_area()
{
double a;
a=x*y;
cout<<" Area of rectangle = "<<a<<endl;
}
};
int main()
{
shape *s[2];
triangle t;
s[0]=&t;
rectangle r;
s[1]=&r;
cout<<" Enter the value of x & y for triangle: ";
s[0]->get_data();
cout<<" Enter the value of x & y for rectangle: ";
s[1]->get_data();
s[0]->display_area();
s[1]->display_area();
return 0;
}
OUTPUT
Enter the value of x & y for triangle: 12 26