Sunteți pe pagina 1din 8

Homework Title / No.

: ___Homework 2__

Course Code: __CAP320_______

Course Instructor: ______Lect. Sucharu Mahajan___

Date of Allotment: _03/2/2011__

Date of submission : _27/2/2011__

Student’s Roll No.____RE3004B42_________

Section No. : _______E3004______________

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 :
__Ramneet kaur__

Evaluator’s comments:
_____________________________________________________________________

Marks obtained : ___________ out of ______________________


PART-A

Ques: -1 How is a class different from a structure and union?

Ans: - Classes are the building blocks of Object Oriented Programming in C++
language. A class is an organization of data and functions which operate on them.The
combination of data members and member functions constitute a data object or simply an
object. It allows the data to be hidden if necessary from external use. By default the
members inside the class are private. It is an extension of the idea of structure used in c+
+. The keyword class specifies that what follows is an abstract data type.

Class class_name //declare class


{
access_specifier_1:
data_member;
access_specifier_2:
member_function;
...
} object_names;

Structure is a collection of only data items. The data item can be of


different type, some can be of int, some can be of float, and some can be of char and so
on. The data item of structure is called member of the structure. A structure is a kind of
blueprint specifying what information is necessary for the program. A union is an object
similar to a structure except that all of its members start at the same location in memory.
A union variable can represent the value of only one of its members at a time.

Struct structure_name //declare structure


{
Declaration_of_data_member;
}; // termination of structure

Ques: -2 How is a member function of a class different from a


conventional user defined function? How is the member function of a
class accessed?

Ans: - Difference b/w member function and user defined function: - A class in C++
has various data items and various member functions. First of all, we can declare the
member function of a class only in that class, on the other hand the conventional user
defined function should be declared in the main function or globally outside the main
function. The conventional user defined function can be used in other programs by
calling it, on the other hand the member function can not be called by other program
.s.
The conventional user defined function should
be defined outside the main function and the member function can be defined inside or
outside the own class.
Accessing a member function: - A class has own member function. To access those
function successfully, we have to create an object which is a component of a program.
We have dot (.) operator to associate the object or calling the member function. We have
an example, we have class “test” and its member function getdata(). so we can access the
function like this: -
int main()
{
test f; // f is the object of test class.
f.getdata();
}

Ques: - 3 Define instantiation of a class. Is it possible to assign data


from one object of a class to another object of same class? Explain with
examples.

Ans: -. Instantiation is one of the most important mechanisms for code reuse in object-
oriented programming languages. Instantiation is the use of object classes. An object
class is a set of objects that share a common structure and a common behavior.
Instantiation is fundamentally a word for the use of inheritance. Programmers can define
object classes, define their own kinds of objects, and instantiate them as needed. This is
one of the most important mechanisms for code reuse in object-oriented programming
languages. When a new instance of an object class is created, it has its own set of
instance variables, and it shares the method implementations with other instances of its
class. An object class specifies a set of visible methods, a set of hidden instance variables
and a set of hidden operations that implement the methods. Instance variables can only be
modified indirectly by invoking operations.

If we use copy constructor, then it is possible to assign data from one object of a
class to another object of the same class. A copy constructor is a special constructor in
the C++ programming language used to create a new object as a copy of an existing
object. The first argument of such a constructor is a reference to an object of the same
type as is being constructed, which might be followed by parameters of any type.
#include<iostream.h>
#include<conio.h>
class Complex
{
int real, img;
public:
Complex (int , int);
Complex (const Complex& source); //copy constructor
};
Complex:: Complex (const Complex& source)
{
this.real = source.real;
this.img = source.img;
}
main ()
{
Complex a (2, 3);
Complex b = a; // this invokes the copy constructor
}

Ques: -4. Can a function be overloaded? If yes then how?

Ans: - Yes, function can be overloaded. C++ enables several functions of the same
name to be defined, as long as these functions have different sets of parameters. This
capability is called function overloading. When an overloaded function is called, the C++
compiler selects the proper function by examining the number, types and order of the
arguments in the call. Function overloading is commonly used to create several functions
of the same name that perform similar tasks but on different data types. Example of
function overloading:

#include<iostream.h>
#include<conio.h>
class overload
{
public:
int max(int , int);
float max(float , float);
};
int overload : : max(int num1,int num2)
{
if(num1>num2)
{
return num1;
}
else
{
return num2;
}
}
float overload : : max(float num1,float num2)
{
if(num1>num2)
{
return num1;
}
else
{
return num2;
}
}
int main()
{
overload obj;
cout<<”obj.max(5.4f,8.6f)<<endl;
cout<<”obj.max(19,34)<<endl;
getch();
}
PART- B

Ques: -5 Create 2 classes DM and DB which store the value of distances.


DM stores distances in meters and centimeter and DB store in feet and
inches. WAP that can read values for the class objects and add one
object of DM with another object of Db.Use the friend function to carry
out the addition operation. The object that stores the results may be a
DM object or DB object, depending on the units in which the results are
required.The display should be in the format of feet and inches or
meters and centimeters depending on the object on display.

Ans: -

#include<iostream.h>

#include<conio.h>

class db

int feet;

float inch;
public:

db()

feet = 0;

inch =0;

db(int f , float e)

feet=f;

inch=e;

void show()

cout<<feet;

cout<<inch;

friend void add(db,dm);

};

class dm

int mtr,cm;

public:

dm(int m,int c)
{

mtr=m;

cm=c;

friend void add(db,dm);

};

void add(db dd,dm mm)

float dfeet=dd.feet+dd.inch/12;

float dmtrcm=(mm.mtr+mm.cm/100)*3.28;

float daddfeet=dfeet+dmtrcm;

db res;

res.feet=int(daddfeet);

res.inch=(daddfeet-res.feet)*12.00;

cout<<”Feet=”<<res.feet<<” Inches=”<<res.inch;

void main()

clrscr();

db d1(2,0);

dm m1(1,100);

add(d1,m1);

getch();
}

Ques6: Write a program to demonstrate how the private data members


cannot be accessed by the public member function of the derived class
even though the derived class has been inherited publicly.

Ans:

#include<iostream.h>
#include<conio.h>
class base
{
private:
int a;
public:
int c;
void getd(int e,int f)
{
a=e;
c=f;
}
};
class derive:public base
{
public:
void show()
{
cout<<”it will not be displayed”<<endl;
cout<<a;
cout<<c;
}
};
void main()
{
clrscr();
derive b;
b1.getd(1,51);
b1.show();
getch();
}

******

S-ar putea să vă placă și