Tuesday, June 30, 2009

MI11-01 - C++ (Semester 2)

MI11-01

Marks 20

C++

Question 1: - Explain the need to use copy construction with suitable examples?

Answer: - When a class is being designed, the designer normally supplies the default constructor and the overloaded constructor. For example, to instantiate an objectof class,class1,

Class cl1;

Class cl1(100);

Suppose class 1 cl1;

Class1 cl2(cl1);

To prevent this member wise copy, it is necessary to provide the copy constructor. Due to the member wise copy, both objects have pointers pointing at the same memory location, resulting in one object being dependent on the other. Therefore, it is necessary to provide a copy constructor in this class.

#include

#include

#include

class string

{private:

char*str;

public:

string() {str=new char[1];strcpy(str,”\0”);}

string(char*s) {str=new char[strlen(s)+1]; strcpy(strs);}

string(string &s {str=strdup(s.str);cout<<”\ncopy called”;}

show() {cout<< endl << str;}

change(char*ss{strcpy(str, ss);}

string&operator=(string&s)

{

str=strdup(s.str);

cout<<”\n assignment called”;return(*this);

}

~string(){delete str;cout<<”\ndestroy”;}

};

main()

{

clscr();

string s1(“welcome”);

strings2=s1;

strings3(s2);

strings4;

s4=s1;

s1.show();

s2.show();

s3.show();

s4.show();

s1.change(“\t changed”);

s1.show();

s2.show();

s3.show();

s4.show();

getch();}

Question 2: - Write short notes on:

· Inheritance: it is the process of creating new classes, called derived classes, from existing, the exiting class is the base class. The derived class inherits all the capabilities of the base class but can add properties of its own. The base class is unchanged by this process. Inheritance has two important advantages.

1. Code reusability: it permits code reusability. Once a base class is written and debugged, it need not be checked again, but can nevertheless be adapted to work in different situations. Reusing code saves time and money and increases a program’s reliability. A programmer can use a class created by another person or company, and without modifying it, derive other classes from it that are suited to particular situations.

2. The derived class can add data and function members to the class without affecting the base class behavior. This is data abstraction.

3. The protection can be increased in derived classes, but cannot decreased, that is – a class item declared as protected in a class cannot be made public later in the derived classes. Making a public item protected or private in a derived class is allowed.

· Polymorphism: it means “any forms”. In C++, the meaning of polymorphism is the ability to access different implementation of a function using the same name. There are two levels at which polymorphism operates. Compile–time polymorphism and run-time polymorphism. Compile-time polymorphism has already been demonstrated through operator overloading and function overloading. Both are subtle form of polymorphism since both cases a single entity is used to refer to two or more things. Whereas in run-time polymorphism is based on virtual functions, derived classes and base class pointers.

-----------------------------xxx--------------------------xxx--------------------------------------

MI11-02

Marks 20

C++

Question 1: Explain containership using an example.

Ansewer1: - In inheritance, if a class B is derived from class A, B has all characteristics of A addition to its own. Hence we can say “B is a kind of A”. There is another kind of relationship called a “has a” relationship. In OOP, this relation occurs when you have an object of class A in class B. For example: -

Containership with student and marks

#include

#include

class student

{

private : int id; char name[20];

public : student()

{ id=0; strcpy(name, ”\0”);

student(int i, char*n)

{id = I; strcpy (name,n)

void getdata()

{cout<<”\n Roll number”; cin>>id;

cout<<”\n Name:”;cimn.get()cin.getline(name,20);

}

void displaydata()

{cout<<”\nId =”<< id<< ”\nname=”<< name;}

};class marks {

private : int mks[3]; student s;

public : marks() {mks[0]=mks1]=mks[2]=0;}

marks(inta,intb,intc) {mks[0]=a;mks[1]=b; mks[2]=c;}

void getdata() { s.getdata();

for (inti=0;i<3;i++)

{count<<”\n Enter marks”<>mks[i];}

}

void dispdata()

{

s.dispdata();

for(int=0;i<3,i++)

{cout<<’\n marks in subject”<< i+1<<”is”;cout<< mks[i];}

}

};

void main()

{ marks m1;

mi.getdata();m1.dispdata();}

Question 2: write short notes on:

· The C++ pointer rule: - the rule as given in C++ terms as follows. A pointer declared as pointing to base class can be used to point to an object of a derived class of that base class, but a pointer to a derived class cannot be used to point to an object of the base class or to any of the derived classes of the base class.

· Pure virtual functions: - virtual functions are functions, which are declared with the key word virtual in the base class.

Example

Virtual void dispdata() {cout<<”this is the base class”;}

These virtual functions may or may not be implemented again in the derived classes. If it is implemented , the declaration must be identical must be identical to that of the base class xcept for the word, virtual.

Void dispdat() {cout<<”roll number””<< roll;}

------------------------------------xxx---------------------------------------xxx------------------

0 Comments:

Search for More Assignments and Papers Here ...

Google
 
 

Interview Preparation | Placement Papers