(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:
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.
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.
Syntax of structure
struct structure_name
{
data_type member1;
data_type member2;
.
.
data_type memeber;
};
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:
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 */
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;
cin<<acno;
cout>>enter amount;
cin<<amt;
cin<<code;
if(code==1)
for(i=0;i<20;i++)
if((c[i].accno==acno)&&(c[i].balance<100))
Q2.(a) Ans
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.
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
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()
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
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:
Q3.(a) Ans
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.
Q3.(b)
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:
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:
Q3.(a) Ans
Q3.(b)
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
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.
class 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
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
{
};
};
Hierarchical inheritance
In this type of inheritance, multiple derived classes inherits from a single base class.
class A
{
};
class B: public A
{
};
class C: public A
{
};
Hybrid inheritance
class A
{
};
class B
{
};
};
class D: public A
{
};
};
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;
};
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.
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:
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.
A function template starts with keyword template followed by template parameter/s inside < >
which is followed by function declaration.
Multiple inheritance:
Multiple inheritance
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
{
};
};
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.