1. What do you
understand by procedural programming paradigm?
Ans. Procedural programming
paradigm lays more emphasis on procedure or the
algorithm. Data is considered secondary. Data is loosely available to many
functions.
2. What do you
understand by object oriented programming paradigm?
Ans.
Object oriented programming paradigm is superset of object based programming.
It offers all the features of object based programming and overcomes its
limitation by implementing inheritance so that the real world relations among
objects can be represented programmatically.
3. Explain all the
features of OOPs with their implementation in C++.
Ans. Abstraction : It refers to the act of representing essential features without
including the background details or explanation.
Encapsulation
is the way of implementing abstraction.
Encapsulation : It is the way of combining both data and the
function that operate on the data under a single unit .
Modularity : It is the property of a system that has been decomposed into a set of cohensive and loosely
coupled modules. Modularity in c++ is
implement through separately compiled files.
Inheritance : It is the capability of one class of thinks to
inherits capability or properties from
another class . Inheritance is implement in c++ by specifying the name
of the (base) class from which the class being defined (the derived class) has
to inherits from.
Polymorphism : It is the ability for a message or data to be
processed in more than one from polymorphism is implemented through virtual
overloaded function and overloaded operation.
4. Why main function
is so special in C++. Give at least two reasons.
Ans. Whenever a C++ program is
executed only the main ( ) is executed
.i.e., execution of the program starts and ends at main ( ). The main ( ) is the driver function of the
program. If it is not present in a
program, no execution took place.
5. Write two
advantages of using #include complier directive.
Ans. i) The #include compiler directive lets us include desired headers
files in our program which enables us work with all declarations inside the
included headers files.
ii) It supports modularity i.e., a bigger program can be decomposed in
terms of header files and later included through this directive.
1. Differentiate
between a run time error and syntax error. Give one example of each.
Ans. A run time error is an error that occurs during execution of a program. The compilation of the program is
not affected. For example, ‘files could not be opened ’ ,’not enough memory
available’ are Run time errors.
A syntax error is that when statements are wrongly written violating
rules of the programming language
For example, Max+2 = D Max is a syntax
error as an expression cannot appear on the left side of an assignment
operator.
7.llustrate the concept of function
overloading with the help of an example.
Ans. A function name having several definitions that are differentials by
the number or types of their arguments is known as function overloading.
For example, following code
overloads a function area to compute area of circle, rectangle and triangle
Float area (float radius) // computes area of circle
{ return 3.14*radius*radius;
}
Float area (float length, float
breadth)
{return length*breadth;
}
Float area (float side1, float
side2, float side3)
{ float& =
(side1+side2+side3) / 2
Float ar = sqrt (&*
(&_side1)*(&_side2)*(&_side3));
Return ar;
}
8.Why is a destructor function required in
classes? Illustrate with the help of
an example.
Ans. Destructor is required in classes because a destructor destroys the
objects that have been created by a constructor. It destroys the values of the
object being destroyed. For example,
#include<iostream.h>
Class xyz
{
Int a,b;
Public;
Void read( );
Xyz ( ) // constructor
{
a=0;
b=0;
}
~xyz ( ) // destructor
{
Cout<<”/n
Destructor”;
}
}
9.What is the use of a constructor function in
a class? Give a suitable example for a constructor function in a class.
Ans. Constructor function is required in classes to create the object. It
is used to initialize the data members of the class. For example,
#include<iostream.h>
Class xyz
{
Int a,b;
Public;
Void read( );
Xyz ( ) // constructor
{
a=0;
b=0;
}
}
10.What is copy
constructor? Explain by giving example.
Ans. A copy constructor is that constructor which is used to initialize
one object with the values of another object of the same class during
declaration. For example,
class TTime
{ Int year, day, month, hour, minute;
Public :
TTime( int y, int m,
int d, int min, int h; )
Void display (void);
};
// copy constructor
TTime : : TTime( int y, int m,
int d, int min, int h; )
{
Year = y , month = m , day = d
, hour = h , minute = min ;
}
11.What is the difference between constructor
and destructor?
Ans. A constructor is a special
initialization function that is called automatically whenever an instance of
your class is declared. The name of the constructor is same that of the class
and return no value.
Destructor is the opposite of the
constructor in the sense that it is invoked when an object ceases to exit. It
also has a same name as that of class but with a prefix ‘~’.
12.What is the purpose of a header file in a
program?
Ans. The purpose of a
header files in a program is to provide function prototypes, definitions of
library functions, declarations
of data types & constants used with the library functions.
13.What do you understand about a base class & a derived class ? If a base class and a derived class each
include a member function with the same name and arguments, which member
function will be called by the object of the derived class if the scope operator
is not used?
Ans. Inheritance is the capability
of one class of thing to inherit capabilities or properties for another class
or derived class. The class whose properties are inherited, are known as base
class. A class inheriting its properties from another class, is known as
subclass.
If a base class and a derived class
each includes a member function with the same name and arguments, the member
function of the class is called if the scope operator is not used.
14.What do you
understand about a member function? How does a member function differ from an
ordinary function?
Ans. i) Member functions have full
access privilege to both the public and private members of the class. While, in
general, nonmember functions have access only to the public member of the
class.
ii) Member functions are defined within
the class scope that is they are not visible outside the scope of class.
Nonmember functions are also visible outside the scope of the class.
15.Differentiate
between call by value & call by reference with a suitable example.
Ans. In call by value method, the
called function creates its own copies of the original values sent to it. Any
changes, that are made occur on the called function’s copy of values and are
not reflected back to the called function.
For example,
Void main ( )
{
int a = 5;
Cout<< “a=”<<a;
Change (a);
Cout<<”/n a=”<<a;
}
Void change (int b)
{
b = 10;
}
Output will be:
a=5
a=5;
In call by reference method, the called
function accesses and works with the original values using their references .
Any changes, that occur, take place on the original value and are reflected
back to the calling code.
For example,
Void main ( )
{
int a =5;
Cout<<” a=”<< a;
Change (a);
Cout<<”/n a=”<<a;
}
Void change ( int & b)
{
B = 10;
}
Output will be:
a=5
a=10
16. Differentiate
between global & local variable with a suitable example.
Ans. Scope of global variables is the
entire program and scope of the local variable is the function which declares
them. The global variable lifetime is the program-run.the lifetime of local
variables having function scope is the function run.
For example, consider the following
code,
#include<iostream.h>
Int x;
Void main ( )
{
int y;
Cout<<x<<y; }
Void check ( )
{
int z;
Cout<<x<<z;
}
17. Differentiate
between nested class & inheritance with a suitable example.
Ans. NESTED CLASS - A class declared within another
class.
For example,
#include<iostream.h>
#include<conio.h>
Class outer
{
int a;
class inner
{
int b;
public:
int c;
void prn (void)
{ cout<<end<<”inner::prn(
)”<<endl;
Cout<<b<<”,”<<c;
}
Inner ( )
{
b=5;
c =10;
}
};
Inner ob1;
Public:
Inner ob2;
Void second (void)
{
Cout<<endl<<”outer::second ()”<<endl;
Cout<<ob2.c<<endl;
Cout<<”A=”<<a<<endl;
Ob1.prn ( );
}
Outer ( )
{
a=25;
}
};
Void main ( )
{
Outer ab ( );
ab.second ( );
ab.ob1.prn ( );
}
INHERITANCE – it is the capability of one class of
things to inherit capabilities or
properties from another class.
For example:
Properties of human.
18.Differentiate
between default & copy constructor with a suitable example.
Ans. Default destructor is one, which accepts no
parameters. For example,
Class Employee
{
Private:
Int a ;
Public :
Employee (
) //
default destructor
{
a=0;
}
}
Copy constructor is that
constructor which is used to initialize one object with the values from another
object of same class during declaration.
For example,
class TTime
{ Int year, day, month, hour, minute;
Public :
TTime( int y, int m,
int d, int min, int h; )
Void display (void) ;
};
// copy constructor
TTime : : TTime( int y, int m,
int d, int min, int h; )
{
Year = y , month = m , day = d
, hour = h , minute = min ;
}
19. What are advantages of OOPs over procedural programming?
Ans. 1) Oops is closer to real world model.
2) Hierarchical relationships among objects can
be well-represented through inheritance.
3) Data can be made
hidden or public as per the need. Only the necessary data is exposed enhancing
the data security.
4) Private data is accessible only through
designed interface in a way suited to the program.
5) Increased modularity
adds to ease to program development.
20. Illustrate
the use of #define in C++ to define a macro.
Ans. A macro refers, to a #define
definition that is used to define words
to enhance readability and understandability. Before compilation the preprocessors replaces every
occurrence of macros as per the
definition e.g.,
#define EOF 0
Will replace every occurrence of EOF with 0
within program.
21. Illustrate
the use of inline function in C++ with the help of a suitable example.
Ans. Inline functions are short that
are actually called, rather, their code is expanded (inline) at the point of
function call. The member function of a class, if defined within the class
definition are inline by default. It defined outside the function can be made
explicitly inline by placing the keywords inline before its definition. For
example, in the following example printSq ( ) is an inline function.
Class Abc
{
Int x;
Public:
Void printSq( )
{
Cout<<x*x;
}
};
22.What is a
default constructor? How does it differ from destructor?
Ans. A default constructor is the one
that takes no arguments. It is automatically invoked when an object when an
object is created without giving any initial value. In case , a user has not
defined a default constructor, the compiler automatically generates it.
But destructors are used to de
initializes the objects.
23. Differentiate
between a date type struct & a data type class in C++.
Ans. Struct data type is similar
to class data type in the sense that
both allow various data items to be combined under one roof. However, the
difference is that in struct data type only data members are combined whereas
class data type lets you combine data as well as their associated function. We
say, a structure is a class without functions. The class data type can
represent real-world entities as it can represent their characteristics and
their behavior.
24. Explain
the concept constructor overloading with a suitable example.
Ans. Constructor overloading
refers to a class having multiple constructor.
Constructor definitions each having a different signature.
For example:
Class ABC
{
A ( ) { }
A (int m) { }
A (int m, int n) { }
A (int m, float n) { }
A (int m, float n, int b) { }
}
Void main ( )
{
int p, q;
cin>>p>>q;
A.a1;
A.a2 (p, q)
A.a3 (p, 2.5, q)
}
25.What do you
understand by visibility modes in class derivations? What are these modes?
Ans. That controls the visibility and
availability of a member function in a class.
These are:-
i)
Public visibility modes
ii)
Private visibility modes
iii)
Protected visibility modes
26.What do you
understand by Syntax Error, logical Error & Run time error?
Ans. A syntax error is that when
statements are wrongly written violating
rules of the programming language.
A run time error is an error that occurs during execution of a program.
The compilation of the program is not affected.
A logical error an error which occur because of wrong interpretation
of logic.
27.Define the
term #define & typedef with suitable example.
Ans. Using type def does not actually create a new data class, rather it defines a new name for a existing
type.
Void main ( )
{
typedef float amount;
amount loan, saving, installment;
.
.
.
.
getch ( );
}
#define allows us to define symbolic names and constants.
#include<iostream.h>
#defineP1 3.14159
Void main ( )
{
int r =10;
float cir;
cir = P1*(r*r);
cout<<”area of
circle”<<cir<<endl;
}
Error (2or 3
marks)
1. #include<iostream.h>
void main()
{
const
MAX=0;
int
a,b;
cin<<a>>b;
if(a>b)
MAX=a;
for(x=0,x<MAX;x++)
cout<<x;
}
Ans. #include<iostream.h>
#include<conio.h>
Void main ( )
{
Clrscr ( );
int MAX = 0;
int a,b;
cin>>a>>b;
if (a>b)MAX = a;
for(int x =0;
x<MAX; x++) cout<<x;
getch ( );
}
2. #include<iostream.h>
Main()
{
int
ch=9,sch=90;
char
S[2,2];
if
ch<=9
cout<<ch;
for(int
x=0;x<2;x++)
for(int
y=0;y<2;y++)
{
if(y==0)
S[x][y]=’A’;
else
cout>>S[x][y];
}
getch();
}
Ans. #include<iostream.h>
#include<conio.h>
Void main ( );
{
Clrscr( );
int ch=9, sch=90;
char[2,2];
if (ch<=9)
cout<<ch;
for (int
x=0;x<2;<x++)
for (int
y=0;y<2;y++)
{
If(y++0)
S[x][y];
Else
Cout<<S[x][y];
}
Getch ( );
}
3. class X
{
public:
int
a,b;
void
int(void)
{
a=b=0;
}
int
sum(void);
int
square(void);
};
int sum(void)
{
return a+b;
}
int square(void)
{
return sum() *sum();
}
Ans. class X
{ public:
int a,b;
void int(void)
{
a=b=0;
}
int sum (void);
int square (void);
};
X int: : sum (void)
{
return a+b;
}
X int :: square (void)
{
return sum ( )*sum (
);
}
4. include<iostream.h>
void main()
{
int
R; W=90;
while
W>60
{
R=W-50;
switch(W)
{ 20:cout<<”Lower
range”<<endl;
30:
cout<<”Middle Range”<<endl;
20:
cout<<”Higher Range”<<endl;
}
}
}
Ans. #include<iostream.h>
#include<conio.h>
Void main ( )
{
int R; W=90;
while (W>60)
{
R = W-50;
Switch(R-10)
{
Case
20: cout<<endl;
5. class ABC
{
int
x=10;
float
y;
ABC(){
y=5; }
~AB?C()
{}
};
void main()
{
ABC
a1,a2;
}
Ans. #include<iostream.h>
#include<conio.h>
class ABC
{
int x;
float y;
public:
ABC( )
{x=10;y=5;}
~ABC ( ) { }
};
Void main ( )
{
ABC a1, a2;
}
6. #include(iostream.h)
void main()
{
int
X[]={60,50,30,40},Y; count=4;
cin>>Y;
for(i=count-1;i>=0;i--)
switch(i)
{
case 0;
case 2: cout<<Y*Y[i]<<endl;
break;
case 1:;
case 3:
cout>>Y+X[i];
}
}
Ans. #include<iostream.h>
#include<conio.h>
Void main ( )
{
Clrscr ( );
int X [ ] =
{60,50,30,40},Y,count = 4;
cin>>Y;
for(int
i=count-1;i>=0;i--)
switch (i)
{
case0;
case2:
cout<<”Y*X[i]<<endl;
break;
case1:;
case3:
cout<<Y+X[i];
}
getch ( );
}
7.struct group
{
int
x1,x2;
}
void main()
{
g1,g2
group;
cin>>g1.x1<<g2.x2;
g2=g1;
cout<<g2.x2<<g2.x1<<endl;
2+=g1.x1;
cout<<g1.x1<<g1.x2;
}
Ans. #include<iostream.h>
#include<conio.h>
Struct group
{
int x1, x2;
}
Void main ( )
{
groupg1, g2;
cin>>g1.x1>>g2.x2;
g2 = g1;
cout<<g2.x2<<g2.x2<<endl;
g1.x1+=2;
cout<<g1.x1<<g1.x2;
getch ( );
}
8.structure swimmingclub
{
int
mem number;
char
mamname[20]
char
memtype[]=”LIG”;
};
void main()
{
swimmingclub per1,per2;
cin<<”Member Number”;
cin>>memnumber.per1;
cout<<”\n Member name”;
cin>>per1.membername;
per1.memtype=”HIG”;
per2=per1;
cin<<”\n Member number
”<<per2.memnumber;
cin<<”\n Member name
“<<per2.memname;
cin<<”\n Member number
“<<per2.memtype;
}
Ans. struct swimmingclub
{
Int mem number;
char mamname[20];
char memtype[];
};
void main()
{
swimmingclub per1,per2;
cin>>”Member Number”;
cin>>per1.memnumber;
cout<<”\n Member name”;
gets(per1.membername);
strcpy(per1.memtype=”HIG”);
per2=per1;
cout<<”\n Member
number”<<per2.memnumber;
cout<<”\n Member name
“<<per2.memname;
cout<<”\n Member number
“<<per2.memtype;
}
9.#include<iostream.h>
CLASS STUDENT
{
int
admno;
float
marks;
public
:
STUDENT()
{
admno=0;
marks=0.0;
}
void
input()
{
cin>>admno;
cin>>marks;
}
void
output()
{
cout<<admno;
cout<<marks;
}
}
void main()
{
STUDENT s;
input(s);
output(s);
}
Ans.
#include<iostream.h>
#include<conio.h>
CLASS STUDENT
{
int admno;
float marks;
public :
STUDENT()
{
admno=0;
marks=0.0;
}
void input()
{
cin>>admno;
cin>>marks;
}
void output()
{
cout<<admno;
cout<<marks;
}
};
void main()
{
clrscr ( );
STUDENT
s;
s.input
( );
s.output ( );
getch ( );
}
10.#include<iostream.h>
void main()
{
struct
STUDENT
{
char
stu_name[20];
char
stu_sex;
int
stu_age=17;
}student;
gets(stu_name);
gets(stu_sex);
}
Ans. #include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
Clrscr ( );
struct STUDENT
{
char stu_name[20];
char stu_sex;
int stu_age;
}student;
gets(student.stu_name);
cin>>student.stu_sex;
cin>>student.stu.age;
getch ( );
}
11. #include <iostream.h>
struct Pixels
{ int
Color,Style;}
void ShowPoint(Pixels P)
{ cout<<P.Color,P.Style<<endl;}
void main()
{
Pixels Point1=(5,3);
ShowPoint(Point1);
Pixels Point2=Point1;
color.Point1+=2;
ShowPoint(Point2);
}
Ans.
#include <iostream.h>
struct Pixels
{
int Color,Style;}
void ShowPoint(Pixels P)
{
cout<<P.Color<<P.Style<<endl;}
void main()
{
Pixels Point1(5,3);
ShowPoint(Point1);
Pixels Point2=Point1;
Point1.colour+=2;
showPoint(Point2);
}
12. #include [iostream.h]
class PAYITNOW
{
int Charge;
PUBLIC:
void Raise(){cin>>Charge;}
void Show{cout<<Charge;}
};
void main()
{
PAYITNOW P;
P.Raise();
Show();
}
Ans. #include
<iostream.h>
#include<conio.h>
class PAYITNOW
{
int
Charge;
public:
void Raise( ){cin>>Charge;}
void Show(
){cout<<Charge;}
};
void main()
{
Clrscr ( );
PAYITNOW P;
P.Raise();
P.Show();
getch ( );
}
13.
#include<iostream.h>
type def int integer;
struct number
{
integer a [5];
}
void main()
{
number x;
for(int i=0;i<5;i++)
cin>>x[i].a;
getch();
}
Ans. #include<iostream.h>
#include<conio.h>
typedef int integer;
struct number
{
integer a [5];
};
void main()
{
Clrscr ( );
number x;
for(int i=0;i<5;i++)
cin>>x[i].a;
getch();
}
Find Output of following :
1. #include<iostream.h>
int &max (int &x,int &y)
{
if(x>y)
return
(x);
else
return
(y);
}
void main()
{
int
A=10,B=13;
max(A,B)=-1;
cout<<”A=
“<<A<<”B= “<<B<<endl;
max(B,A)=7;
cout<<”A=
“<<A<<”B= “<<B<<endl;
}
Ans.
A = -1
B = 13
A = 7
B = 13
2. #include<iostream.h>
int a=3;
void demo(int x,
int y,int &z)
{
a+=x+y;
z=a+y;
y+=x;
cout<<x<<”, “<<y<<”,
“<<z<<endl;
}
void main()
{
int a=2,b=5;
demo(::a,a,b);
cout<<::a<<”,
“<<a<<”, “<<b<<endl;
demo(::a,a,b);
}
Ans.
3,5,10
8,2,10
8,10,20
3. #include<iostream.h>
int max(int
&x,int &y,int &z)
{
if(x>y &&y>z)
{
y++;
z++;
return x;
}
else
if(y>x)
return y;
else
return z;
}
void main()
{
int a=10,b=13,c=8;
a=max(a,b,c);
cout<<a<<b<<c<<endl;
b=max(a,b,c);
cout<<++a<<++b<<++c<<endl;
}
Ans. 13138
1499
4. #include<iostream.h>
struct point
{
int x,y;
};
void show(point
p)
{
cout<<p.x<<’;’<<p.y<<endl;
}
void main()
{
point U={0,10},V,W;
V=U;
V.x+=0;
W=V;
U.y+=10;
U.x+=5;
W.x-=5;
show(U);
show(V);
show(W);
}
Ans.
5, 20
0, 10
5, 10
5) #include <iostream.h>
void
Changethecontent(int Arr[], int Count)
{
for
(int C=1;C<Count;C++)
Arr[C-1]+=Arr[C];
}
void main()
{
int
A[]={3,4,5},B[]={10,20,30,40},C[]={900,1200};
Changethecontent(A,3);
Changethecontent(B,4);
Changethecontent(C,2);
for
(int L=0;L<3;L++) cout<<A[L]<<’#’;
cout<<endl;
for
(L=0;L<4;L++) cout<<B[L] <<’#’;
cout<<endl;
for
(L=0;L<2;L++) cout<<C[L] <<’#’;
}
Ans. 7#9#5#
30#50#70#40
2100#1200#
6) #include <iostream.h>
struct Game
{
char
Magic[20];int Score;
};
void main()
{
Game
M={“Tiger”,500};
char
*Choice;
Choice=M.Magic;
Choice[4]=’P’;
Choice[2]=’L’;
M.Score+=50;
cout<<M.Magic<<M.Score<<endl;
Game N=M;
N.Magic[0]=’A’;N.Magic[3]=’J’;
N.Score-=120;
cout<<N.Magic<<N.Score<<endl;
}
Ans.
TiLeP550
AiLJP120
7) In the following program, if
the value of N given by the user is 20, what maximum and minimum values the
program could possibly display?
#include
<iostream.h>
#include
<stdlib.h>
void main()
{
int N,Guessnum;
randomize();
cin>>N;
Guessnum=random(N-10)+10;
cout<<Guessnum<<endl;
}
Ans.
Minimum value = 10
Maximum value = 19
8) #include<iostream.h>
class student
{
public:
student()
{
cout<<”\n
Computer Science“;
}
~student()
{
cout<<” subject”;
}
}st;
void main()
{
cout<<”
is my best“
}
Ans. Computer Science is my best subject
9) In the following C++ program , what will the
maximum and minimum value of r generated
with the help of random function. 2
#include<iostream.h>
#include<stdlib.h>
void main()
{
int r;
randomize();
r=random(20)+random(2);
cout<<r;
}
Ans.
Minimum value = 10
Maximum value = 20
10) void
Modify(int &a,int b=10)
{
if(b%10==0)
a+=5;
for(int
i=5;i<=a;i++)
cout<<b++<<”:”;
cout<<endl;
}
void Disp(int x)
{
if(x%3==0)
Modify(x);
else
Modify(x,3);
}
void main()
{
Disp(3);
Disp(4);
Modify(2,20);
}
Ans. 10:11:12:13:
20:21:22:
11) What will be the
output of following program:
# include
<iostream.h>
# include<conio.h>
int area(int s)
{return (s*s);}
float area(int b,int
h)
{
return (0.5*b*h);
}
void main()
{clrscr();
cout<<area(7)<<endl;
cout<<area(4,3)<<endl;
cout<<area(6,area(3))<<endl;
getch();
}
Ans. 49
6
27
12) #include<iostream.h>
#include<string.h>
void main()
{
char mystring [] = “What@OUTPUT!”;
for(int i=0; mystring[i] != ‘\0’; i++)
{
If(!isalpha(mystring[i]))
mystring[i]=’*’;
else if
(isupper(mystring[i]))
mystring[i]= mystring[i]+1;
else
mystring[i]= Mystring[i+1];
}
cout<<
mystring;
}
Ans. Xat@* PVUQVU*
13)
void main()
{
clrscr( );
char *name=" a
ProFiLe";
for(int
x=0;x<strlen(name);x++)
{
if(islower(name[x]))
name[x]=toupper(name[x]);
else
if(isupper(name[x]))
if(x%2!=0)
name[x]=tolower(name[x-1]);
else
name[x]--;
}
cout<<name<<endl;
getch();
}
Ans.
A OROoLiE
Classes &
constructor
1. Answer the
questions(i) and (ii) after going through the following class :
class Exam
{
int
year;
public
:
Exam(int
y) { year=y; }
Exam(Exam
&t);
}
a. Create an object,
such that it invokes constructor 1.
b. Write complete
definition for constructor 2.
Ans. a) Exam
obj1 ( 2006 );
b) Exam ( Exam & t )
{
Year = t.year ;
}
2. Define a class
named Housing in C++ with the following descriptions :
private members
reg_no
integers (Ranges 10-1000)
name char array
type
character
cost
float
public members
i.
function
Read_data() to read an object of housing type.
ii.
function
display() to display the details of an object.
iii.
function
draw_nos() to choose and display the details of 2 houses selected randomly from
an array of 10 objects of type Housing. Use random function to generate the
registration no with reg_no from the array.
Ans. class
HOUSING
{
Private:
Int REG_NO;
Char NAME [50];
Char TYPE;
Float COST;
Public:
Void Read_Data ( );
Void Display ( );
Void Draw_Nos ( );
};
Void HOUSING: : Read_Data ( )
{
Cout<<”\n Enter
Reg No (10-1000):”;
Cin>>REG_NO;
Cout<<”Name”;
Gets(NAME);
Cout<<”Type”;
Cin>>TYPE;
Cout<<”Cost”;
Cin>>COST;
}
Void HOUSING: : Display ( )
{
Cout<<”\n Reg _ No
:”<< REG _ NO;
Cout<<”\n Name
:”<< NAME;
Cout<<”\n Type
:”<< TYPE;
Cout<<”\n Cost
:”<< COST;
}
Void HOUSING: : Draw _ Nos ( )
{
Int no1, no2, i;
randomize ( );
no1 = random (991) +10;
no2 = randome (991) +10;
for(i=0; i<10; i++)
i((Arr[i].REG_NO = =
no1)//(Arr[i].REG_NO = = no2) )
Display ( );
}
3. Define a class
named Directory in C++ with the following descriptions :
private members
docunames string (documents name in
directory)
freespace long (total number of bytes
available in directory )
occupied long (total number of bytes available
in directory)
public members
newdocuentry() a function to accept values of
docunames,freespace & occupied from user
retfreespace() a function that return the
value of total kilobytes available. (1 KB=1024 b)
showfiles() a
function that displays the names of all the documents in directory.
Ans. The class is as:
Class director
{
Char docuentry[10][25];
Long Freespace;
Long occupied;
Public :
Void Newdocuentry( );
Long Retfreespace( );
Void showfiles( );
};
Void Directory: : Newdocuentry( )
{
Cout<<”Enter the names of the documents”;
For(int i=0; i<10; i++)
Cin>>docunames[i];
Cout<<”Enter the filespace
available”;
Cin>> Freespace;
Cout<<”Enter the space occupied”:
Cin>>occupied;
}
Long Directory: :Retfreespace( )
{
Return (Freespace/1024);
}
Void Directory: :Showfiles( )
{
For(int i=0;i<10; i++)
Cout<<docunames[i]<<endl;
}
4. Define a class
named Publisher in C++ with the following descriptions :
private members
Id
long
title
40 char
author
40 char
price
, stockqty double
stockvalue double
valcal() A function to find price*stockqty
with double as return type
Public members
iv.
a constructor
function to initialize price , stockqty and stockvalue as 0
v.
Enter() function
to input the idnumber , title and author
vi.
Takestock()
function to increment stockqty by N(where N is passed as argument to this
function) and call the function valcal() to update the stockvalue().
vii.
sale() function
to decrease the stockqty by N (where N is sale quantity passed to this function
as argument) and also call the function valcal() to update the stockvalue
viii.
outdata()
function to display all the data members on the screen.
Ans. The class is as:
class publisher
{
long ID;
char title[40];
char author[40];
double
price,stockqty;
doule stockvalue;
double valcal()
{
Return
price*stockqty;
}
public:
publisher() {
price=0,stockqtv=0’stockvalue=0;}
void enter()
{
cin>>”enter
the id number”>>ID;
cin>>”enter
the title “>>gets(title);
cin>>”enter
the author name”>>gets(author);
}
void Takestock(int N)
{
stockvalue=stockqty+N;
valcal();
}
void sale(int N)
{
stockvalue=stockqty-N;
valcal();
}
void outdata()
{ cout<<ID;
cout<<title;
cout<<author;
cout<<stockvalue;
}
5. Define a class
named Serial in C++ with the following descriptions :
private members
serialcode int
title 20 char
duration float
noofepisodes integer
Public members
ix.
a constructor
function to initialize duration as 30 and noofepisodes as 10.
x.
Newserial()
function to accept values for serialcode and title.
xi.
otherentries()
function to assign the values of duration and noofepisodes with the help of
corresponding values passed as parameters to this function.
xii.
dispdata()
function to display all the data members on the screen.
Ans. The class is as:
Class serial
{
Private :
Int serialcode;
Char title[20];
Float Duration;
Int Noofepisodes;
Public :
Serial( )
{
Duration = 30;
Noofepisode = 10;
}
Void Newserial ( )
{
Cout<<”enter
serialcode”;
Cin>>
serialcode;
Cout<<”enter
serial title”;
Gets(title);
}
Void Otherenteries( int x ,
int y )
{
Duration = x;
Noofepiosde = y;
}
Void dispdata( )
{
Cout<<”Serial
code”<<serialcode<<endl;
Cout<<”Serial
title”<<title<<endl;
Cout<<”Duration
of the serial”<<Duration<<endl;
Cout<<”No.
of episode”<<Noofepisode<<endl;
}
};
6. Define a class Competition in C++ with the following
descriptions:
Data
Members
Event_no integer
Description char(30)
Score integer
qualified char
Member
functions
A constructor to assign initial values
Event_No number as 101,Description as
“State level” Score is 50 , qualified
‘N’.
Input() To take the input for
event_no, description and score.
Award(int) To award qualified as ‘Y’, if score
is more than the cut off score passed as argument to the function else ‘N’.
Show() To display all the details.
Ans.
The class is :
Class COMPETITON
{
Private:
Int
event no;
Char
description;
Int
score;
Char
qualified;
Public:
Void
input( )
{
Cout<<”Enter event no.”;
Cin>>event_no;
Cout<<”Enter description;
Gets(description);
Cout<<”Enter score”;
Cin>>score;
}
Void
award( int cut_off)
{
If(score>cut_off)
Qualified = ’y’;
Else
Qualified = ‘N’;
}
Void show( )
{
Cout<<”event_no<<”\t”<description<<”\t”<<score<<”|t”<<qualified;
}
};
7. Declare a class bank to represent bank account of 10 customers with the following
data members: name of depositor, account
number, type of account(s for savings and c for current account),
balance amount. The class also contains the following member functions:
(a) To initialize data members.
(b)
To deposit money
(c)
To withdraw money after checking minimum balance (say
1000)
(d)
To display the
data members on screen.
Ans. The class is as:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
// class declaration
Class account
{
Char name[25],type;
Public :
Float balance;
Int accno;
Void init ( );
Void show ( );
Void despo ( );
Void withdr ( );
Account ( )
{
bal = 0;
}
~account ( )
{ }
};
Void account: : init ( )
{
Cout<<”\n\n\tEnter the name”;
Gets(name);
Cout<<”\n\tYour account type (s)avings or (c)urrent”;
Cin>>type;
Cout<<”\n\tDone
! Your account number is:”;
Bal = 1000;
}
Void account: :show( )
{
Cout<<:\n\n\tNAME:”<<name;
Cout<<”\n\tACCOUNT NO. :”<<accno;
Cout<<”\n\tACCOUNT
TYPE :”<<type;
Cout<<”\n\tCURRENT
BALANCE :Rs.”<< bal<<”/-“;
}
Void account : : despo( )
{
Float temp;
Cout<<”\n\tEnter
te amount to be deposited”:
Cin>>temp;
Bal+=temp;
}
Void account : :withdr( )
{
Float temp;
Cout<<”\n\tEnter
the amount to be withdraw;
Cin>>temp;
if((bal-temp)<1000)
cout<<”\n\tSorry! You cannot withdraw the money”;
else
Bal-=temp;
}
Void main ( )
{
Account acc;
Acc.init( );
Acc.withdr( );
Acc.show( );
}
8. Define a class named Garments in C++ with
following description? 4
Private members:
Gcode of
type string
Gtype of
type string
Gsize of
type integer
Gfabric of type string
G Price of type float A function
Assign( ) which calculate and assigns
the value of GPrice gets reduced by 10%.
Public members:
A constructor to
assign initial values of Gcode, Gtype and Gfabric with the word “NOT ALLOTED”
and Gsize and GPrice with 0.
A function input(
) to input the values of the data members Gcode, Gtype ,Gsize and Gfabric and
invoke the Assign( ) function.
A function to
Display ( ) which displays the content of all the data members for a Garment.
Ans.
The class is as:
Class garments
{ Char GCode[15];
Char GType[15];
Int Gsize;
Char GFabric[15];
Float GPrice;
Void Assign( )
{ if(strcmp(GFabric,”COTTON”==0)
{ if (strcmp(GType,”TROUSER”==0)
GPrice=1300;
Else
if(strcmp(GType,”SHIRT”==0)
GProce=1100;
}
Else
{ if(strcmp(GType,”TROUSER”==0)
GPrice = 1300 - 0.10 * 1300;
Else if(strcmp(GType,”SHIRT”==0)
GPrice = 1100 – 0.10 *1100;
}
}
Public:
Garments( )
{ Strcpy(GCode,”NOT ALLOUTED”);
Strcpy(GType,”NOT
ALLOUTED”);
Strcpy(GFabric,”NOT ALLOUTED”);
GSize = 0;
GPrice = 0;
}
Void Input( )
{
Cout<<”Enter
Garment Code”;
Cin>>GCode;
Cout<<”\n
Enter Garment Type(TROUSER/SHIRT)”;
Cin>>GType;
Cout<<”\n
Enter Garment Size”;
Cin>>GSize;
Cout<<”\n
Enter Garment Fabric”;
Gets(GFabric);
Assign(
);
}
Void Display( )
{ cout<<”Garment
Code:”<<GCode<<endl;
Cout<<”Garment Type:”<<GType<<endl;
Cout<<”Garment Size:”<<GSize<<endl;
Cout<<”Garment Fabric:”<<GFabric<<endl;
Cout<<”Garment
Price:”<<GPrice<<endl;
}
};
No comments:
Post a Comment