Assignment Cse 251 T

Post on 28-Apr-2015

18 views 0 download

Transcript of Assignment Cse 251 T

ASSIGNMENT 1

Name: Prasoon Srivastava Submitted To:

Roll No.: B20 Omkara Murthy

Sec: E1004

Course code: CSE 251-T

Ans.1.

When a derived class is created from the base class it carries the attributes of the base class, that is the data members which are public to base class becomes the data member of the derived class. Now if we try to assign the object of derived class to base class, base class has insufficient data members to copy the data variables of derived class.

This is overcome in OOPs and the excess data members of derived class are sliced away copying the data members of derived class to base class. This concept of slicing away the extra data members of derived class is known as Object Slicing.

A short program to demonstrate Object Slicing:

//Object slicing example

#include<iostream.h>

#include<conio.h>

using namespace std;

class parent{

public:

int i,j;

parent(){i=j=0;}

};

class child:public parent{

public:

int m,n;

child(){m=n=0;}

};

int main()

{

parent p;

child c;

c.i=1;

c.j=3;

c.m=8;

c.n=6;

p=c;//The members m and n are sliced away leaving i and j to be copied

cout<<"Values of parent class is: "<<p.i<<" "<<p.j;//Output we get for i and j

getch();

return 0;}

Ans.2.

//Program for simulation of Concept of delegation

#include<iostream.h>

#include<conio.h>

using namespace std;

class one{

public:

void show_E()

{

cout <<"Program in class one."<< endl;

}

};

class two{

one *e;

public:

void show_F()

{

e->show_E();

cout <<"Program in class two."<< endl;

}

};

int main()

{

two f;

f.show_F();

getch();

return 0;

}

Ans.3.

//Program for the solution of Problem no.3

#include<iostream.h>

#include<conio.h>

using namespace std;

float tot_pr=0.0;//Total price of products

class exp_date{

int day,mon,yr;

public:

exp_date(){}

void date(){cout<<"Enter expiry date(dd/mm/yy): ";

cin>>day>>mon>>yr;

cout<<"\n";}

void show(){cout<<"Expiry date: ";

cout<<day<<"/"<<mon<<"/"<<yr<<"\n"<<endl;}

};

class PRODUCT{

int item_code;

char name[50];

float pr;

exp_date exd;

public:

//Default assigning data member values using constructors

PRODUCT(){item_code=0;

pr=0.0;}

void input_data();

void display_data();

void display_data(int);//Searching will be done on the basis of Item Code

void bill(); //Also function is overloaded for performing search operation

};

void PRODUCT::input_data()

{

cout<<"Enter item code: ";

cin>>item_code;

cout<<"Enter name: ";

cin>>name;

cout<<"Enter price: ";

cin>>pr;

tot_pr+=pr;

exd.date();

}

void PRODUCT::display_data()

{

cout<<"Item code: "<<item_code<<endl;

cout<<"Name of product: "<<name<<endl;

cout<<"Price of product: "<<pr<<endl;

exd.show();

}

void PRODUCT::display_data(int n)

{

if(item_code==n)

display_data();

}

void PRODUCT::bill()

{

cout<<item_code<<"\t\t"<<name<<"\t\t"<<pr<<endl;

}

int main()

{

PRODUCT *P=new PRODUCT[10];

int i,sr,nop;

char ch;

cout<<"Enter number of products to be entered(max:10): ";

cin>>nop;

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

P[i].input_data();

cout<<"Display items(y/n): ";

cin>>ch;

if(ch=='y'||ch=='Y')

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

P[i].display_data();

ch='n';

do{

cout<<"\nEnter item code to be searched: ";

cin>>sr;

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

P[i].display_data(sr);

cout<<"Want to search another(y/n): ";

cin>>ch;

}while(ch=='y'||ch=='Y');

cout<<"\n *****Generating Bill*****\n";

cout<<"\nItem code Name Price\n";

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

P[i].bill();

cout<<"\nTotal money to pay :"<<tot_pr<<endl;

delete[] P;

getch();

return 0;

}

Ans.4.

a) Yes, we can use multiple constructors for the creation of object. This can be done while we want to create object either through initialization or without initialization.Even we can use as many initializations as we want to do.//A short program using 2 constructors demonstrating the use of multiple constructors#include <iostream>using namespace std;

class myclass { int x;public: // overload constructor two ways myclass() { x = 0; } // no initializer myclass(int n) { x = n; } // initializer

int getx() { return x; }};

int main(){ myclass o1(10); // declare with initial value myclass o2; // declare without initializer

cout << "o1: " << o1.getx() << '\n'; cout << "o2: " << o2.getx() << '\n';

return 0;}

b) The concept of reusability can be implemented in C++ while dealing with inheritance. When we create a general class whose some functions are common to other class created further the concept of reusability can be implemented.

For ex. We have vehicle having same characteristic of number, having engine etc.And we create a car as its sub class, and truck as its sub class having their unique characteristic as well as general vehicle characteristic. This saves the writing time and uses reusability.That is:class vehicle{ public: char engine[50],company[25]; ……………. ………………};

class car:public vehicle{ public: char parts[25];……………..……………

};class truck:public vehicle{ public: int ntyre;………….………….};

Here vehicle serves as base class an writing the name of engine and model again for the class of car and truck is saved.

The derived class has the memory space of its data members as well as the data members it is deriving class. That is the memory allocation would be same as would have been when reusability is not done.

Ans.5.

Operator Overloading:

Operator overloading is concept of polymorphism in which an operator is overloaded to perform application on user defined object as well.

Like an operator say ‘+’ is used to add operands like int, float, numbers etc.

But after overloading the operator its application can be extended to user defined data type such as class.

However some operators like . :: .* ?: cannot be overloaded.

The general form of operator overloading is:

Return-type class-name::operator#(arg-list)

{

//operations to be performed

}

//Program for the solution of problem no.5

#include<iostream.h>

#include<conio.h>

using namespace std;

class vehicle

{

int veh_num;

char model[25],type[25];

float cost;

public:

void input();

void display();

void operator==(vehicle);//Comparison of two vehicles

};

void vehicle::input()

{

cout<<"Enter the vecihle type :";

cin>>type;

cout<<"Enter the model :";

cin>>model;

cout<<"Enter the vehicle number :";

cin>>veh_num;

cout<<"Enter cost of vehicle :";

cin>>cost;

cout<<"\n";

}

void vehicle::display()

{

cout<<"Vehicle type :"<<type<<endl;

cout<<"Vehicle model :"<<model<<endl;

cout<<"Vehicle number :"<<veh_num<<endl;

cout<<"Vehicle cost :"<<cost<<endl;

cout<<"\n";

}

void vehicle::operator==(vehicle V2)

{

cout<<"S.No. Vehicle type Vehicle model Number Cost\n";

cout<<"V1 "<<type<<" "<<model<<"\t\t"<<veh_num<<"\t\t"<<cost<<endl;

cout<<"V2 "<<V2.type<<" "<<V2.model<<"\t\t"<<V2.veh_num<<"\t\t"<<V2.cost<<endl;

}

int main()

{

vehicle *V=new vehicle[10];

int nov,v1,v2,i;//v1 and v2 are for comparison of vehicals by number

char ch;

cout<<"Enter number of vehicles to be entered(Max:10): ";

cin>>nov;

for(i=0;i<nov;i++)//nov-number of vehicles

V[i].input();

cout<<"Display vehicles(y/n): ";

cin>>ch;

if(ch=='y'||ch=='Y')

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

V[i].display();

cout<<"Enter two vehicles to be compared(in terms of indices:0-"<<nov-1<<"): ";

cin>>v1>>v2;

V[v1]==V[v2];//Comparison using operator overloading

getch();

return 0;

}

Ans.6.

Dynamic Binding:

Dynamic binding occurs when a pointer or reference is associated with a member function based on the dynamic type of the object. The dynamic type of an object is the type of the object actually pointed or referred to rather than the static type of its pointer or reference.

The member function that is dynamically bound must override a virtual function declared in a direct or indirect base class.

Since dynamic binding occurs at run time, it is also called run time binding.

//Program to show advantage of dynamic memory allocation

#include<iostream.h>

#include<conio.h>

using namespace std;

int main()

{

int num,size,p1[100];

cout<<"Enetr memory space to be used(no. of integer variables): ";

cin>>num;

//The rest integer variables not used only consumes memory space

int *p=new int[num];//Dynamically assigning memory i.e. at run time

//int p[num] is an invalid syntax

size=sizeof(p1);//Size of memory created by static memory allocation rising 400 bytes

cout<<"Size of static memory created "<<size<<" bytes.\n";

size=sizeof(p);//Size of memory created by dynamic memory allocation saves memory

cout<<"Size of dynamic memory created "<<size<<" bytes.\n";

delete[] p;

p=0;

getch();

return 0;

}