Model Answer Winter 2019
Model Answer Winter 2019
Model Answer Winter 2019
22316-Model-Answer-Winter-2019
Page 1 / 21
Diagram
1M
Page 2 / 21
Example
1M
Page 3 / 21
a class definition. Since all the objects belonging to that class use the
same member functions, no separate space is allocated for member Descript
functions. When the objects are created only space for member ion 2M
variable is allocated separately for each object. Separate memory
locations for the objects are essential because the member variables
will hold different data values for different objects.
Diagram
2M
Page 4 / 21
Page 5 / 21
program.
6. It is easy to partition the work in a project based on objects.
7. The data-centered design approach enables us to capture more
details of a model in implementable form.
8. Object-oriented systems can be easily upgraded from small to
large systems.
9. Message passing techniques for communication between objects
makes the interface descriptions with external systems much
simpler.
10. Software complexity can be easily managed.
d) Describe ‘this’ pointer with an example. 4M
Ans. ‘this’ pointer:
C++ uses a unique keyword called „this‟ to represent an object that
invokes a member function. This unique pointer is automatically
passed to a member function when it is invoked. „this‟ is a pointer
that always point to the object for which the member function was Descript
called. ion 2M
For example, the function call A.max ( ) will set the pointer „this‟ to
the address of the object A. Then suppose we call B.max ( ), the
pointer „this‟ will store address of object B.
Example:
#include<iostream.h>
class sample
{
int a; Correct
public: example
void setdata(int x) 2M
{
this ->a=x;
}
void putdata()
{
cout<<this ->a;
}
};
void main()
{
sample s;
Page 6 / 21
s.setdata(100);
s.putdata( );
}
In the above example, this pointer is used to represent object s when
setdata ( ) and putdata ( ) functions are called.
3. Attempt any THREE of the following: 12
a) Write the applications of object oriented programming. 4M
Ans. Applications of object oriented programming are:
1) Real time systems
2) Simulation and modeling Any
3) Object-oriented databases four
4) Hypertext, hypermedia and expertext correct
5) AI and expert systems applicati
6) Neural networks and parallel programming ons 1M
7) Decision support and office automation systems each
8) CIM/CAM/CAD systems
b) State the rules for writing destructor function. 4M
Ans. Rules for writing destructor function are:
1) A destructor is a special member function which should destroy
the objects that have been created by constructor. Any
2) Name of destructor and name of the class should be same. four
3) Destructor name should be preceded with tilde (~) symbol. correct
4) Destructor should not accept any parameters. rules
5) Destructor should not return any value. 1M each
6) Destructor should not be classified in any types.
7) A class can have at most one destructor.
c) What is inheritance? Give different types of inheritance. 4M
Ans. Inheritance:
The mechanism of deriving new class from an old/existing class is
called inheritance. Correct
OR explanat
Inheritance is the process by which objects of one class acquired the ion of
properties of objects of another classes. inherita
nce 2M
Syntax:
Page 7 / 21
Types of inheritance:
1) Single inheritance: In single inheritance, a derived class is
derived from only one base class.
Diagram:
Correct
types of
inherita
nce
2) Multiple inheritance: In multiple inheritance, derived class is (any 4)
derived from more than one base classes. 2M
Diagram:
Page 8 / 21
Page 9 / 21
Page 10 / 21
Page 11 / 21
Example:
Program to interchange values of two integer numbers using
friend function.
#include<iostream.h>
#include<conio.h>
class B; Correct
class A example
{ 2M
int x;
public:
void accept()
{
cout<<"\n Enter the value for x:";
cin>>x;
}
friend void swap(A,B);
};
class B
{
int y;
public:
void accept()
{
cout<<"\n Enter the value for y:";
cin>>y;
}
friend void swap(A,B);
};
void swap(A a,B b)
Page 12 / 21
{
cout<<"\n Before swapping:";
cout<<"\n Value for x="<<a.x;
cout<<"\n Value for y="<<b.y;
int temp;
temp=a.x;
a.x=b.y;
b.y=temp;
cout<<"\n After swapping:";
cout<<"\n Value for x="<<a.x;
cout<<"\n Value for y="<<b.y;
}
void main()
{
A a;
B b;
clrscr();
a.accept();
b.accept();
swap(a,b);
getch();
}
d) Write a program to count the number of lines in file. 4M
(Note: Any other correct logic shall be considered)
Ans. #include<iostream.h>
#include<fstream.h> Opening
#include<conio.h> of file
void main() 1M
{
ifstream file; Countin
char ch; g
int n=0; number
clrscr(); of lines
file.open("abc.txt"); 2M
while(file) Printing
{ number
file.get(ch); of lines
if(ch=='\n') in a file
n++; 1M
Page 13 / 21
}
cout<<"\n Number of lines in a file are:"<<n;
file.close();
getch();
}
5. Attempt any TWO of the following: 12
a) Write a program to declare a class ‘student’ having data 6M
members as ‘stud_name’ and ‘roll_no’. Accept and display this
data for 5 students.
(Note: Any other correct logic shall be considered)
Ans. #include<iostream.h>
#include<conio.h>
class student
{
int roll_no;
char stud_name[20]; Class
public: declarati
void Accept(); on 2M
void Display();
};
void student::Accept() Accept
{ ( )1M
cout<<"\n Enter student‟s name and roll no\n";
cin>>stud_name>>roll_no;
}
void student::Display()
{ Display
cout<<stud_name<<”\t”<<roll_no<<”\n”; ( ) 1M
}
void main()
{
student S[5];
inti;
clrscr();
for(i=0;i<5;i++)
{ Main ( )
S[i].Accept(); with
} array
cout<<”Student details \n Student‟s Name \t Roll No\n”; 2M
Page 14 / 21
for(i=0;i<5;i++)
{
S[i].Display();
}
getch();
}
b) State and explain the visibility modes used in inheritance. 6M
Ans. Visibility modes:
private
protected
public
Base class Derived class visibility
visibility Private Protected Public
Private Not Not Not
Inherited Inherited Inherited
Protected Private Protected Protected
Public Private Protected Public
Private:
o When a base class is privately inherited by a derived class,
„public members‟ and „protected members‟ of the base class
become „private members‟ of the derived class.
o Therefore, the public and protected members of the base class
can only be accessed by the member functions of derived class
but, cannot be accessed by the objects of the derived class.
Syntax: Explana
class derived: private base tion 2M
{ for each
//Members of derived class; visibility
}; mode
Public:
o When a base class is publicly inherited by a derived class then
„protected members‟ of base class becomes „protected
members‟ and ‟public members‟ of the base class become
„public members‟ of the derived class.
o Therefore the public members of the base class can be
accessed by both the member functions of derived class as well
Page 15 / 21
Protected:
o When a base class is protectedly inherited by a derived class,
„public and protected members‟ of the base class become
„protected members‟ of the derived class.
o Therefore the public and protected members of the base class
can be accessed by the member functions of derived class as
well as the member functions of immediate derived class of it
but they cannot be accessed by the objects of derived class
Syntax:
class derived: protected base
{
//Members of derived class;
};
c) Write a program to declare a class ‘book’ containing data 6M
members as ‘title’, ‘author-name’, ‘publication’, ‘price’. Accept
and display the information for one object using pointer to that
object.
(Note: Any other correct logic shall be considered)
Ans. #include<iostream.h>
#include<conio.h>
class book
{ Class
char author_name[20]; declarati
char title[20]; on 2M
char publication[20];
float price;
public:
void Accept();
void Display();
};
void book::Accept() Accept
{ ( ) 1M
Page 16 / 21
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
clrscr();
ifstream fs; File
ofstream ft; open
char ch, fname1[20], fname2[20]; and
cout<<"Enter source file name with extension (like files.txt) : "; close
gets(fname1); 2M
fs.open(fname1);
Page 17 / 21
if(!fs)
{
cout<<"Error in opening source file..!!";
getch();
exit(1); Logic
} for copy
cout<<"Enter target file name with extension (like filet.txt) : "; contents
gets(fname2); 4M
ft.open(fname2);
if(!ft)
{
cout<<"Error in opening target file..!!";
fs.close();
getch();
exit(2);
}
while(fs.eof()==0)
{
fs>>ch;
ft<<ch;
}
cout<<"File copied successfully..!!";
fs.close();
ft.close();
getch();
}
b) Write a program to implement the following hierarchy using 6M
suitable member functions. Refer Figure No.2.
Page 18 / 21
void display_test()
{
cout<<”\n test Marks \n Marks1 \t Marks2 \n”;
cout<<marks1<<”\t”<<marks2;
}
};
class sports
{
int score;
Page 19 / 21
public:
void read_sportsData()
{
cout<<”\n Enter sport score\n”; Class
cin>> score; sports
} declarati
void display_sportsData() on 1M
{
cout<<”\n sport score:”<<score;
}
};
class result: public test, public sports
{
int total;
public:
void read_result()
{
read_ studentData () ;
read_test(); Class
read_sportsData(); result
total=marks1+marks2; declarati
} on 2M
void display_result()
{
display_ studentData () ;
display_test();
display_sportsData();
cout<<”\n Total=”<<total;
}
};
void main()
{
result r;
clrscr();
r.read_result(); Main ()
r.display_result(); 1M
getch();
}
Page 20 / 21
Page 21 / 21