0% found this document useful (0 votes)
62 views7 pages

Content of Homework Should Start From This Page Only

The document appears to be a homework assignment submission form for a student named Gurbachan Singh. It includes fields for the homework title/number, course code/instructor, date assigned/submitted, student roll number and section number. The student has signed a declaration of original work. Space is provided for evaluator comments and the marks obtained. The content of the homework is to begin on this page.

Uploaded by

Jagjit Saul
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views7 pages

Content of Homework Should Start From This Page Only

The document appears to be a homework assignment submission form for a student named Gurbachan Singh. It includes fields for the homework title/number, course code/instructor, date assigned/submitted, student roll number and section number. The student has signed a declaration of original work. Space is provided for evaluator comments and the marks obtained. The content of the homework is to begin on this page.

Uploaded by

Jagjit Saul
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Homework Title / No.

: ________________HW-3_______Course Code : ____CAP 202_____

Course Instructor : ______________________ Course Tutor (if applicable) : ____________

Date of Allotment : _____________________ Date of submission : ___________________

Student’s Roll No._______________________ Section No. : _________________________

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 : __
____gurbachan singh_

Evaluator’s comments:
_____________________________________________________________________

Marks obtained : ___________ out of ______________________

Content of Homework should start from this page only:


Qns:1- . Discuss the concept of inheritance in detail and differentiate all types of inheritance forms.
Ans: inheritance is a way to compartmentalize and reuse code by creating collections of
attributes and behaviors called objects which can be based on previously created objects. In
classical inheritance where objects are defined by classes, classes can inherit other classes. The
new classes, known as subclasses (or derived classes), inherit attributes and behavior of the pre-
existing classes, which are referred to as superclasses (or ancestor classes). The inheritance
relationships of classes gives rise to a hierarchy. In prototype-based programming, objects can
be defined directly from other objects without the need to define any classes, in which case this
feature is called differential inheritance.

Different types of inheritance:

1. Single class Inheritance:


When class a gas inherited in class has known as base class and B class is know asderived class.
Here only two classes have linked to each other.

2. 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 there quirement. For example, class A
inherited in class b and class b has inherited inclass c for class b so on. Where class A is base
class c. In another way we can say bis derived class a base class for c and a indirect base class for
c is indirect baseclass for c and c indirect derived class for class A.

3. Multiple Inheritances:
In this type of inheritance, number of classes has inherited in a single class. Wheretwo or more
classes are, know as base class and one is derive class

4. Hierarchical Inheritance:
This type of inheritance helps us to create a baseless for number of classes andthose numbers of classes can
have further their branches of number of class.
5. Hybrid Inheritance:
In this type of inheritance, we can have mixture of number of inheritances but thiscan generate an
error of using same name function from no of classes, which willbother the compiler to how to use the
functions. Therefore, it will generate errors inthe program. This has known as ambiguity or
duplicity.

Qns:2- Elaborate on the importance of pointers in c++.


Ans: A pointer is a programming language data type whose value refers directly to (or "points
to") another value stored elsewhere in the computer memory using its address. For high-level
programming languages, pointers effectively take the place of general purpose registers in low-
level languages such as assembly language or machine code, but may be in available memory. A
pointer references a location in memory, and obtaining the value at the location a pointer refers
to is known as dereferencing the pointer. A pointer is a simple, less abstracted implementation of
the more abstracted reference data type. Several languages support some type of pointer,
although some have more restrictions on their use than others.Pointers to data significantly
improve performance for repetitive operations such as traversing strings, lookup tables, control
tables and tree structures. In particular, it is often much cheaper in time and space to copy and
dereference pointers than it is to copy and access the data to which the pointers point.
Pointers are also used to hold the addresses of entry points for called subroutines in procedural
programming and for run-time linking to dynamic link libraries (DLLs). In Object-oriented
programming, pointers to functions are used for binding methods, often using what are called
virtual method tables.While "pointer" has been used to refer to references in general, it more
properly applies to data structures whose interface explicitly allows the pointer to be manipulated
as a memory address, as opposed to a magic cookie or capability where this is not possible.
Because pointers allow both protected and unprotected access to memory addresses, there are
risks associated with using them particularly in the latter case.
Qns:3- 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 anode. 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;};

Qns:4- What are virtual functions? Explain with programming example.


Ans: In object-oriented programming, a virtual function or virtual method is a function or
method whose behavior can be overridden within an inheriting class by a function with the same
signature. This concept is a very important part of the polymorphism portion of object-oriented
programming (OOP).
Example:-
class Animal
{
public:
virtual void eat() const
{
std::cout << "I eat like a generic Animal." << std::endl;
}
virtual ~Animal()
}
};

class Wolf : public Animal


{
public:
void eat() const
{
std::cout << "I eat like a wolf!" << std::endl;
}
virtual ~Wolf() {
}
};

int main()
{
std::vector<Animal*> animals;
animals.push_back(new Animal());
animals.push_back(new Wolf());

for (std::vector<Animal*>::const_iterator it = animals.begin(); it != animals.end(); ++it)


{
(*it)->eat();
delete *it;
}
return 0;
}

Qns:5- WAP to show the overloading of assignment operator.


Ans:
class My_Array
{
int * array;
int count;
public:
My_Array & operator= (const My_Array & other)
{
if (this != &other) // protect against invalid self-assignment
{
// 1: allocate new memory and copy the elements
int * new_array = new int[other.count];
std::copy(other.array, other.array + other.count, new_array);
// 2: deallocate old memory
delete [] array;
// 3: assign the new memory to the object
array = new_array;
count = other.count;
}
// by convention, always return *this
return *this;
}
};

You might also like