Anda di halaman 1dari 19

Q1.

(a) Homogeneous data: Homogeneous data structures are those data structures that contain
only similar type of data e.g. like a data structure containing only integer or float values. The
simplest example of such type of data structures is an Array.

Heterogeneous Data: Heterogeneous Data Structures are those data structures that contains a
variety or dissimilar type of data, for e.g. a data structure that can contain various data of
different data types like integer, float and character. The examples of such data structures include
structures, union etc.

Features of structure:

Structure is a collection of different data type elements or variable.


Structure is a user defined data type.
Structure is the collection of variables of different types under a single name for better handling.

For example: You want to store the information about student about his/her name, course and
age. You can create these information separately but, better approach will be collection of these
information under single name because all these information are related to person.

If you want to access structure members in C, structure variable should be declared.

Many structure variables can be declared for same structure and memory will be
allocated for each separately.

It is a best practice to initialize a structure to null while declaring, if we dont assign any
values to structure members.

Keyword struct is used for creating a structure.

Syntax of structure

struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeber;

};

We can create the structure for a Student as mentioned above:


When a structure is defined, it creates a user-defined type but, no storage is allocated. For the
above structure of Student, variable can be declared as:
struct student
{
char name[20];
int age;
char course[20];};
Q1.(b) Structures as Function Arguments

You can pass a structure as a function argument in very similar way as you pass any other
variable or pointer. You would access structure variables in the similar way as you have accessed
in the above example:

For example we want find maximum age of five students


#include<stdio.h>
#include<conio.h>

struct student
{
char name[20];
int age;
char course[20];
};
int maxage(struct student s[5]);
void main( )
{
int i;
struct student s1[5]; /* Declare s1 variable of structure type student */

/* Input value of student s1 */


for(i=0;i<5;i++)
{
printf(\nEnter Name of Student");
scanf(%s,s1[i].name);
printf(\nEnter age of student);
scanf(%d,&s1[i].age);
printf(\n Enter Course of Student");
scanf(%s,s1[i].couse);
}
/* calling function to calculate maximum age out of five students */
cout<< maximum age out of five students is<<maxage(s1);
getch();
}
int maxage(struct student s[5])
{
int temp;
temp=s[0];
for(i=1;i<5;i++)
{
if(temp<s[i])
temp=s[i];
}
return temp;
}

Q1.(a) Ans:
Structure declaration:

struct customer

accno long;

name char[20];

balance float;

} c[20];

Function to print account number and name of each customer with balance<100

void printdetail()

int i;

for(i=0;i<20;i++)

if(c[i].balance<100)

cout>>c[i].accno>>c[i].name;

void main()

long acno;

float amt;
int code,i;

cout>>enter account no;

cin<<acno;

cout>>enter amount;

cin<<amt;

cout>>enter 1 to deposit and 0 to withdraw;

cin<<code;

if(code==1)

for(i=0;i<20;i++)

if((c[i].accno==acno)&&(c[i].balance<100))

cout>>the balance is insufficient for specified withdrawl;

Q2.(a) Ans

In C++, structure behaves like class like can add function,


and can use properties on class as inheritance, virtual,
etc.
while in C, structure we can have only data member but not
functions.
In "C++" Structure we can specify the data and functions as public,private or protected but in
"C" Structure we can't.

In "C" Structure Variable initialization must contains the keyword struct but in C++ not required.

C Structure :-
1. Only variables of different data types can be declared, functions are not allowed
2. Direct access to data members is possible
3. struct data type is not treated as built in type use of struct necessary to declare objects
4. Member variables cannot be initialized inside a structure

C++ Structure :-
1. In C++ structure declaration functions can also be declared
2. The members declared in a C++ structure is public by default
3. While declaring an object the keyword struct is omitted in C++

Q2.(b)

A container class is a class that is used to hold objects in memory or external storage. A container
class acts as a generic holder. A container class has a predefined behavior and a well-known
interface. A container class is a supporting class whose purpose is to hide the topology used for
maintaining the list of objects in memory. When a container class contains a group of mixed
objects, the container is called a heterogeneous container; when the container is holding a group
of objects that are all the same, the container is called a homogeneous container.

A container stores many entities and provide sequential or direct access to them. List, vector and
strings are such containers in standard template library. The string class is a container that holds
chars. All container classes access the contained elements safely and efficiently by using
iterators. Container class is a class that hold group of same or mixed objects in memory. It can be
heterogeneous and homogeneous. Heterogeneous container class can hold mixed objects in
memory whereas when it is holding same objects, it is called as homogeneous container class.

The following are the standardized container classes :


1. std::map :
Used for handle sparse array or a sparse matrix.
2. std::vector :
Like an array, this standard container class offers additional features such as bunds checking
through the at () member function, inserting or removing elements, automatic memory
management and throwing exceptions.
std::string :
A better supplement for arrays of chars.
Proxy Class

A proxy class is a class that acts on behalf of another class. There are many uses for proxy
classes, but generally they are used to simplify a complex object's interface, by hiding details that
are of no relevance within the object's current context. Although it may be better to declare a
stripped-down version of the object, this is only practical when you have access to the original
class implementation (in which case the full-blown class can be derived from the stripped-down
class). Rather than re-invent wheels, accessing an existing class by proxy offers a more
convenient workaround.

Q2.(a) Ans

Accessing data members and functions inside main program

We can access data members and functions of a class by creating object of that class.

With object we can access public data members and functions of a class using dot (.) operator

example

class circle
{
private:
float radius;
public:
void input()
{
cout<<Enter radius;
cin>>radius;
}
void area()
{
cout<<Area=<<(3.147*radius*radius);
}
};

void main()

circle ob; //object of class circle

ob.input(); //accessing member function of class circle

ob.area();

}
Access data members and functions inside member function of same class.

We can directly access any member data and functions inside member function of the same class.

Example

class circle
{
private:
float radius;
public:
void input()
{
cout<<Enter radius;
cin>>radius; //access member data of same class
}
void area()
{
cout<<Area=<<(3.147*radius*radius);
}
};

Q2.(b) Ans

An object is a variable of class type (class data type).


Once a class is defined, you can declare variables of that type. A class variable is called object or
instance. In other words, a class would be the data type, and an object would be the variable.

Once a class is defined, you can declare objects of that type. The syntax for declaring a object is
the same as that for declaring any other variable. The following statements declare two objects of
type circle:

circle c1, c2;

Q3.(a) Ans

Operator overloading is an important concept in C++. It is a type of polymorphism in which an


operator is overloaded to give user defined meaning to it. Overloaded operator is used to perform
operation on user-defined data type. For example '+' operator can be overloaded to perform
addition on various data types, like for Integer, String(concatenation) etc.

Operator overloading can be done by implementing a function which can be :

1. Member Function

2. Non-Member Function

3. Friend Function
Operator overloading function can be a member function if the Left operand is an Object of that
class, but if the Left operand is different, then Operator overloading function must be a non-
member function.

Operator overloading function can be made friend function if it needs access to the private and
protected members of class.

Operator Overloading Syntax

Q3.(b)

The two general classes of operators are:

unary A unary operator operates on only one operand. A unary operator typically appears with
its operand in this format:

operator operand

binary A binary operator operates on two operands. A binary operator appears with its operands
in this format:

operand1 operator operand2

The Operators which operate on Single Operand known as Unary Operators, some of the
unary operators are:

++ Increment Operator
-- Decrement Operator
& Address Of Operator
- Unary Minus Operators
~ (Ones Compliment) Negation Operator
! Logical NOT
and so on..

The Operators which operate on Two Operands known as Binary Operators, some of the
binary operators are:

+ Binary Plus Operator


- Binary Minus Operator
== Equal to Operator
< Less than Operator
and so on..

Q3.(a) Ans

Operator overloading is an important concept in C++. It is a type of polymorphism in which an


operator is overloaded to give user defined meaning to it. Overloaded operator is used to perform
operation on user-defined data type. For example '+' operator can be overloaded to perform
addition on various data types, like for Integer, String(concatenation) etc.

Operator Overloading Syntax

Q3.(b)

It is a special type of function in C++.


Friend function is function that is not member of any class but can access private data of that
class whose friend is that function.
Because they are not members of any class, you should not call them using the dot operator.
If a function is defined as a friend function then, the private and protected data of class can be
accessed from that function. The complier knows a given function is a friend function by its
keyword friend. The declaration of friend function should be made inside the body of class (can
be anywhere inside class either in private or public section) starting with keyword friend.

Syntax:
class class_name
{
...... .... ........
friend return_type function_name(argument/s);
...... .... ........
}

Example:
To find the average of two numbers using friend function.
#include<iostream.h>
#include<conio.h>
class base
{
int val1,val2;
public:
void get()
{
cout<<"Enter two values:";
cin>>val1>>val2;
}
friend float avg(base ob);
};
float avg(base ob)
{
return float(ob.val1+ob.val2)/2;
}
void main()
{
clrscr();
base obj;
obj.get();
cout<<"\n Mean value is : "<<ang(obj);
getch();
}

Q4.(a) Ans

The mechanism that allows us to extend the definition of a class without making any physical
changes to the existing class is inheritance.

Inheritance lets you create new classes from existing class. Any new class that you create from
an existing class is called derived class; existing class is called base class.

The inheritance relationship enables a derived class to inherit features from its base class.
Furthermore, the derived class can add new features of its own. Therefore, rather than create
completely new classes from scratch, you can take advantage of inheritance and reduce software
complexity.

In order to derive a class from another, we use a colon (:) in the declaration of the derived class
using the following format :
class derived_class: memberAccessSpecifier base_class
{
...
};

Forms of Inheritance

Single level inheritance

In single level inheritance, there is only one base class and has only one derived class. In this
type of inheritance one derived class inherits from only one base class. It is the simplest form of
Inheritance.

Example of single level inheritance:

class Base
{

};

class Derv: public Base


{

};

Multi-level inheritance

In multi-level inheritance, there will be a chain of inheritance with a class derived from only one
parent and will have only one child class.

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.
Example of multi level inheritance:

class A
{

};

class B: public A
{

};

class C: public B
{

};

Multiple inheritance

One class being derived from multiple parent classes.

In this type of inheritance a single derived class may inherit from two or more than two base
classes.
Example of multiple inheritance:

class A
{

};

class B
{

};

class C: public B, public A


{

};

Hierarchical inheritance

Many classes deriving from one class.

In this type of inheritance, multiple derived classes inherits from a single base class.

Example of hierarchical inheritance:

class A
{

};
class B: public A
{

};

class C: public A
{

};

Hybrid inheritance

It is a mixture of 2 or more of above types of inheritance. There is no pattern of deriving from


classes.

Hybrid Inheritance is combination of Hierarchical and Multilevel Inheritance.

Example of hybrid inheritance:

class A
{

};

class B
{

};

class C: public A, public B


{

};
class D: public A
{

};

class X: public D, public C


{

};

Q4.(b) Ans

The mechanism that allows us to extend the definition of a class without making any physical
changes to the existing class is inheritance.

Inheritance lets you create new classes from existing class. Any new class that you create from
an existing class is called derived class; existing class is called base class.

The inheritance relationship enables a derived class to inherit features from its base class.
Furthermore, the derived class can add new features of its own. Therefore, rather than create
completely new classes from scratch, you can take advantage of inheritance and reduce software
complexity.

Q4.(a) Ans

Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract classes
are used to provide an Interface for its sub classes. Classes inheriting an Abstract Class must
provide definition to the pure virtual function, otherwise they will also become abstract class.

1. Abstract class cannot be instantiated, but pointers and refrences of Abstract class type can
be created.

2. Abstract class can have normal functions and variables along with a pure virtual function.

3. Abstract classes are mainly used for Upcasting, so that its derived classes can use its
interface.

4. Classes inheriting an Abstract Class must implement all pure virtual functions, or else
they will become Abstract too.

Example

class AbstractClass {
public:
virtual void AbstractMemberFunction() = 0; // Pure virtual function makes
// this class Abstract class.
virtual void NonAbstractMemberFunction1(); // Virtual function.

void NonAbstractMemberFunction2();
};

Q4.(b) ans

5.(a) Ans

An ambiguity can arise when several paths exist to a class from the same base class. This means
that a child class could have duplicate sets of members inherited from a single base class.
C++ solves this issue by introducing a virtual base class. When a class is made virtual, necessary
care is taken so that the duplication is avoided regardless of the number of paths that exist to the
child class.

When two or more objects are derived from a common base class, we can prevent multiple
copies of the base class being present in an object derived from those objects by declaring the
base class as virtual when it is being inherited. Such a base class is known as virtual base class.
This can be achieved by preceding the base class name with the word virtual.
Consider the following example :

class A
{
public:
int i;
};

class B : virtual public A


{
public:
int j;
};

class C: virtual public A


{
public:
int k;
};

class D: public B, public C


{
public:
int sum;
};
int main()
{
D ob;
ob.i = 10; //unambiguous since only one copy of i is inherited.
ob.j = 20;
ob.k = 30;
ob.sum = ob.i + ob.j + ob.k;
cout << Value of i is : << ob.i<<\n;
cout << Value of j is : << ob.j<<\n; cout << Value of k is :<< ob.k<<\n;
cout << Sum is : << ob.sum <<\n;

return 0;
}

Q5.(b) Ans

Error is , which we know in advanced. Which we get after building an application or after
compilation. Mostly those are Syntax errors which we can resolve while coding.

But exception is, which occurs at run time , while execution of application. In .NET we can
caught exception and show appropriate message to end user. We can use Try - Catch block to
caught the exception.

An exception is a problem that arises during the execution of a program. A C++ exception is a
response to an exceptional circumstance that arises while a program is running, such as an
attempt to divide by zero.

Exceptions provide a way to transfer control from one part of a program to another. C++
exception handling is built upon three keywords: try, catch, and throw.

throw: A program throws an exception when a problem shows up. This is done using a
throw keyword.

catch: A program catches an exception with an exception handler at the place in a


program where you want to handle the problem. The catch keyword indicates the
catching of an exception.

try: A try block identifies a block of code for which particular exceptions will be
activated. It's followed by one or more catch blocks.

Q5. Ans
Templates:

A template is a blueprint or formula for creating a generic class or a function.

Templates in C++ programming allows function or class to work on more than one data type at
once without writing different codes for different data types. Templates are often used in larger
programs for the purpose of code reusability and flexibility of program. The concept of templetes
can be used in two different ways:

Function Templates

Class Templates

Function Templates

A function templates work in similar manner as function but with one key difference. A single
function template can work on different types at once but, different functions are needed to
perform identical task on different data types. If you need to perform identical operations on two
or more types of data then, you can use function overloading. But better approach would be to
use function templates because you can perform this task by writing less code and code is easier
to maintain.

How to define function template?

A function template starts with keyword template followed by template parameter/s inside < >
which is followed by function declaration.

template <class T> ret-type func-name(parameter list)


{
// body of function
}

Multiple inheritance:

Multiple inheritance

One class being derived from multiple parent classes.

In this type of inheritance a single derived class may inherit from two or more than two base
classes.
Example of multiple inheritance:

class A
{

};

class B
{

};

class C: public B, public A


{

};

Pointer to classes:

A pointer to a C++ class is done exactly the same way as a pointer to a structure and to access
members of a pointer to a class you use the member access operator -> operator, just as you do
with pointers to structures. Also as with all pointers, you must initialize the pointer before using
it.

Objects can also be pointed to by pointers: Once declared, a class becomes a valid type, so it can
be used as the type pointed to by a pointer. For example:

Rectangle * prect;
is a pointer to an object of class Rectangle.

Anda mungkin juga menyukai