You are on page 1of 7

Homework Title / No.

: __________Homework 3____________Course Code : __CAP202

Course Instructor : __Miss Ramandeep Kaur_________ Course Tutor (if applicable) : ____________

Date of Allotment : _____________________ Date of submission : ___04/04/2011 __

Student’s Roll No.___RE3001B72 __________ Section No. : ___RE3001_____________

Declaration:
I declare that this assignment is my individual work. I have not copied from any other student’s work or
from any other source except where due acknowledgment is made explicitly in the text, nor has any part
been written for me by another person.

Student’s Signature : _____________

Evaluator’s comments:
_____________________________________________________________________

Marks obtained : ___________ out of ______________________

Q1. Discuss the concept of inheritance in detail and


differentiate all types of inheritance forms.

Ans:
Inheritance is the process by which new classes called derived classes are created
from existing classes called base classes. The derived classes have all the features
of the base class and the programmer can choose to add new features specific to
the newly created derived class.

l1. Single class Inheritance:


When class a gas inherited in class has known as base class and B class is know as
derived class. Here only two classes have linked to each other.
l2. Multilevel Inheritance:
In this type of inheritance, there are number of level and it has used in that cases
where we want to use all properties in number of levels according to the
requirement. For example, class A inherited in class b and class b has inherited in
class c for class b so on. Where class A is base class c. In another way we can say b
is derived class a base class for c and a indirect base class for c is indirect base
class for c and c indirect derived class for class A.

l3. Multiple Inheritances:


In this type of inheritance, number of classes has inherited in a single class. Where
two or more classes are, know as base class and one is derive class.
l4. Hierarchical Inheritance:
This type of inheritance helps us to create a baseless for number of classes and
those numbers of classes can have further their branches of number of class.

l5. Hybrid Inheritance:


In this type of inheritance, we can have mixture of number of inheritances but this
can generate an error of using same name function from no of classes, which will
bother the compiler to how to use the functions. Therefore, it will generate errors in
the program. This has known as ambiguity or duplicity.

Q2. Elaborate on the importance of pointers in c++.

Ans:

importance of pointers in c++.


1)can return more then 1 value from a function
2)no need to pass the entire object to a function..can pass just the address
3)if we want our object to be modify by some function then we have only option to
pass its address or reference
4)can implement run time polymorphism(virtual...function)
5)in writing driver .....pointer is needed....also can do programming at low level
6)can implement garbage collection thing like reference counting
7)if want to shere some object omng classes we can have the pointer of that object
in each intyerested classes

Q3. Explain with example how pointers are useful in linked lists.

Ans:
An abstract data type (ADT) called a linked list, which is of interest here
because it is implemented using pointers. You will learn much more about
abstract data types in general later in the course.
In the implementation given below, a linked list consists of a series of nodes, each
containing some data. Each node also contains a pointer pointing to the next node
in the list. There is an additional separate pointer which points to the first node, and
the pointer in the last node simply points to "NULL". The advantage of linked lists
over (for example) arrays is that individual nodes can be added or deleted
dynamically, at the beginning, at the end, or in the middle of the list.
In our example, we will describe how to implement a linked list in which the data at
each node is a single word (i.e. string of characters). The first task is to define a
node. To do this, we can associate a string with a pointer using a structure
definition:
struct Node
{
char word[MAX_WORD_LENGTH];
Node *ptr_to_next_node;
};

or alternatively
struct Node;
typedef Node *Node_ptr;

struct Node
{
char word[MAX_WORD_LENGTH];
Node_ptr ptr_to_next_node;
};

Q4. What are virtual functions? Explain with programming


example.
Ans:
C++ virtual function is a member function of a class, whose functionality can be over-ridden in its derived
classes. The whole function body can be replaced with a new set of implementation in the derived class.
The concept of c++ virtual functions is different from C++ Function overloading.

lC++ Virtual Function - Properties:

C++ virtual function is,


• A member function of a class
• Declared with virtual keyword
• Usually has a different functionality in the derived class
• A function call is resolved at run-time
The difference between a non-virtual c++ member function and a virtual member function is, the non-
virtual member functions are resolved at compile time. This mechanism is called static binding. Where
as the c++ virtual member functions are resolved during run-time. This mechanism is known as dynamic
binding.
class Window // Base class for C++ virtual function example
{
public:
virtual void Create() // virtual function for C++ virtual function example
{
cout <<"Base class Window"<
}
};

class CommandButton : public Window


{
public:
void Create()
{
cout<<"Derived class Command Button - Overridden C++ virtual function"<
}
};

void main()
{
Window *x, *y;

x = new Window();
x->Create();

y = new CommandButton();
y->Create();
}

The output of the above program will be,


Base class Window
Derived class Command Button

Q5. WAP to show the overloading of assignment operator.

Ans:

lOverloading assignments operator:


You overload the assignment operator, operator=, with a nonstatic member
function that has only one parameter. You cannot declare an overloaded
assignment operator that is a nonmember function. The following example shows
how you can overload the assignment operator for a particular class:
struct X {
int data;
X& operator=(X& a) { return a; }
X& operator=(int a) {
data = a;
return *this;
}
};

int main() {
X x1, x2;
x1 = x2; // call x1.operator=(x2)
x1 = 5; // call x1.operator=(5)
}

The assignment x1 = x2 calls the copy assignment operator X& X::operator=(X&).


The assignment x1 = 5 calls the copy assignment operator X& X::operator=(int).
The compiler implicitly declares a copy assignment operator for a class if you do not
define one yourself. Consequently, the copy assignment operator (operator=) of a
derived class hides the copy assignment operator of its base class.
However, you can declare any copy assignment operator as virtual. The following
example demonstrates this:
#include <iostream>
using namespace std;

struct A {
A& operator=(char) {
cout << "A& A::operator=(char)" << endl;
return *this;
}
virtual A& operator=(const A&) {
cout << "A& A::operator=(const A&)" << endl;
return *this;
}
};

struct B : A {
B& operator=(char) {
cout << "B& B::operator=(char)" << endl;
return *this;
}
virtual B& operator=(const A&) {
cout << "B& B::operator=(const A&)" << endl;
return *this;
}
};

struct C : B { };

int main() {
B b1;
B b2;
A* ap1 = &b1;
A* ap2 = &b1;
*ap1 = 'z';
*ap2 = b2;

C c1;
// c1 = 'z';
}

The following is the output of the above example:


A& A::operator=(char)
B& B::operator=(const A&)