Professional Documents
Culture Documents
Course Instructor : __Miss Ramandeep Kaur_________ Course Tutor (if applicable) : ____________
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.
Evaluator’s comments:
_____________________________________________________________________
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.
Ans:
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;
};
void main()
{
Window *x, *y;
x = new Window();
x->Create();
y = new CommandButton();
y->Create();
}
Ans:
int main() {
X x1, x2;
x1 = x2; // call x1.operator=(x2)
x1 = 5; // call x1.operator=(5)
}
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';
}