Documente Academic
Documente Profesional
Documente Cultură
: ___Homework 2__
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:
_____________________________________________________________________
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.
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();
}
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
}
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
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;
};
class dm
int mtr,cm;
public:
dm(int m,int c)
{
mtr=m;
cm=c;
};
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();
}
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();
}
******