Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
PART - I, VOL – I
TYPICAL QUESTIONS & ANSWERS
OBJECTIVE TYPE QUESTIONS
Each Question carries 2 marks.
Choose correct or the best alternative in the following:
Q.1 The address of a variable temp of type float is
(A) *temp (B) &temp
(C) float& temp (D) float temp&
Ans: B
Q.2 What is the output of the following code
char symbol[3]={‘a’,‘b’,‘c’};
for (int index=0; index<3; index++)
cout << symbol [index];
(A) a b c (B) “abc”
(C) abc (D) ‘abc’
Ans: C
Q.3 The process of building new classes from existing one is called ______.
(A) Polymorphism (B) Structure
(C) Inheritance (D) Cascading
Ans: C
Q.4 If a class C is derived from class B, which is derived from class A, all through public inheritance, then a class C member function can access
(A) protected and public data only in C and B.
(B) protected and public data only in C.
(C) private data in A and B.
(D) protected data in A and B.
Ans: D
Q.5 If the variable count exceeds 100, a single statement that prints “Too many” is
(A) if (count<100) cout << “Too many”;
(B) if (count>100) cout >> “Too many”;
(C) if (count>100) cout << “Too many”;
(D) None of these.
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
Ans: C
Q.6 Usually a pure virtual function
(A) has complete function body.
(B) will never be called.
(C) will be called only to delete an object.
(D) is defined only in derived class.
Ans: D
Q.7 To perform stream I/O with disk files in C++, you should
(A) open and close files as in procedural languages.
(B) use classes derived from ios.
(C) use C language library functions to read and write data.
(D) include the IOSTREAM.H header file.
Ans: B
Q.8 Overloading the function operator
(A) requires a class with an overloaded operator.
(B) requires a class with an overloaded [ ] operator.
(C) allows you to create objects that act syntactically like functions.
(D) usually make use of a constructor that takes arguments.
Ans: A
Q.9 In C++, the range of signed integer type variable is ________
(A) 0 to
(B)
![]()
(C)
(D) ![]()
Ans: B
Q.10 If
then
equals________.
(where
is a bitwise XOR
operator)
(A) 00000111 (B) 10000010
(C) 10100000 (D) 11001000
Ans: A
Q.11 If an array is declared as
int a[4] = {3, 0, 1, 2}, then values assigned to a[0] & a[4] will be ________
(A) 3, 2 (B) 0, 2
(C) 3, 0 (D) 0, 4
Ans: C
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
Q.12 Mechanism of deriving a class from another derived class is known as____
(A) Polymorphism (B) Single Inheritance
(C) Multilevel Inheritance (D) Message Passing
Ans: C
Q.13 RunTime Polymorphism is achieved by ______
(A) friend function (B) virtual function
(C) operator overloading (D) function overloading
Ans: B
Q.14 A function call mechanism that passes arguments to a function by passing a copy of the values of the arguments is __________
(A) call by name (B) call by value
(C) call by reference (D) call by value result
Ans: B
Q.15 In C++, dynamic memory allocation is accomplished with the operator ____
(A) new (B) this
(C) malloc( ) (D) delete
Ans: A
Q.16 If we create a file by ‘ifstream’, then the default mode of the file is _________
(A) ios :: out (B) ios :: in
(C) ios :: app (D) ios :: binary
Ans: B
Q.17 A variable defined within a block is visible
(A) from the point of definition onward in the program.
(B) from the point of definition onward in the function.
(C) from the point of definition onward in the block.
(D) throughout the function.
Ans: C
Q.18 The break statement causes an exit
(A) from the innermost loop only. (B) only from the innermost switch.
(C) from all loops & switches. (D) from the innermost loop or switch.
Ans: D
Q.19 Which of the following cannot be legitimately passed to a function
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
(A) A constant. (B) A variable.
(C) A structure. (D) A header file.
Ans: D
Q.20 A property which is not true for classes is that they
(A) are removed from memory when not in use.
(B) permit data to be hidden from other classes.
(C) bring together all aspects of an entity in one place.
(D) Can closely model objects in the real world.
Ans: C
Q.21 You can read input that consists of multiple lines of text using
(A) the normal cout << combination.
(B) the cin.get( ) function with one argument.
(C) the cin.get( ) function with two arguments.
(D) the cin.get( ) function with three arguments.
Ans: C
Q.22 The keyword friend does not appear in
(A) the class allowing access to another class.
(B) the class desiring access to another class.
(C) the private section of a class.
(D) the public section of a class.
Ans: C
Q.23 The process of building new classes from existing one is called
(A) Structure. (B) Inheritance.
(C) Polymorphism. (D) Template.
Ans: B
Q.24 If you wanted to sort many large objects or structures, it would be most efficient to
(A) place them in an array & sort the array.
(B) place pointers to them in an array & sort the array.
(C) place them in a linked list and sort the linked list.
(D) place references to them in an array and sort the array.
Ans: C
Q.25 Which statement gets affected when i++ is changed to ++i?
(A) i = 20; i++;
(B) for (i = 0; i<20; i++) { }
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
(C) a = i++;
(D) while (i++ = 20) cout <<i;
Ans: A
Q.26 A friend function to a class, C cannot access
(A) private data members and member functions.
(B) public data members and member functions.
(C) protected data members and member functions.
(D) the data members of the derived class of C.
Ans: D
Q.27 The operator that cannot be overloaded is
(A) ++ (B) ::
(C) ( ) (D) ~
Ans: B
Q.28 A struct is the same as a class except that
(A) there are no member functions.
(B) all members are public.
(C) cannot be used in inheritance hierarchy.
(D) it does have a this pointer.
Ans: C
Q.29 Pure virtual functions
(A) have to be redefined in the inherited class.
(B) cannot have public access specification.
(C) are mandatory for a virtual class.
(D) None of the above.
Ans: A
Q.30 Additional information sent when an exception is thrown may be placed in
(A) the throw keyword.
(B) the function that caused the error.
(C) the catch block.
(D) an object of the exception class.
Ans: C
Q.31 Use of virtual functions implies
(A) overloading. (B) overriding.
(C) static binding. (D) dynamic binding.
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
Ans: D
Q.32 this pointer
(A) implicitly points to an object.
(B) can be explicitly used in a class.
(C) can be used to return an object.
(D) All of the above.
Ans: D
Q.33 Within a switch statement
(A) Continue can be used but Break cannot be used
(B) Continue cannot be used but Break can be used
(C) Both Continue and Break can be used
(D) Neither Continue nor Break can be used
Ans:B
Q.34 Data members which are static
(A) cannot be assigned a value
(B) can only be used in static functions
(C) cannot be defined in a Union
(D) can be accessed outside the class
Ans:B
Q.35 Which of the following is false for cin?
(A) It represents standard input.
(B) It is an object of istream class.
(C) It is a class of which stream is an object.
(D) Using cin the data can be read from user’s terminal.
Ans:C
Q.36 It is possible to declare as a friend
(A) a member function (B) a global function
(C) a class (D) all of the above
Ans:D
Q.37 In multiple inheritance
(A) the base classes must have only default constructors
(B) cannot have virtual functions
(C) can include virtual classes
(D) None of the above.
Ans:C
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
Q.38 Declaration of a pointer reserves memory space
(A) for the object.
(B) for the pointer.
(C) both for the object and the pointer.
(D) none of these.
Ans:B
Q.39 for (; ;)
(A) means the test which is done using some expression is always true
(B) is not valid
(C) will loop forever
(D) should be written as for( )
Ans:C
Q.40 The operator << when overloaded in a class
(A) must be a member function (B) must be a non member function
(C) can be both (A) & (B) above (D) cannot be overloaded
Ans:C
Q.41 A virtual class is the same as
(A) an abstract class (B) a class with a virtual function
(C) a base class (D) none of the above.
Ans:D
Q.42 Identify the operator that is NOT used with pointers
(A) -> (B) &
(C) * (D) >>
Ans:D
Q.43 Consider the following statements
char *ptr;
ptr = “hello”;
cout << *ptr;
What will be printed?
(A) first letter (B) entire string
(C) it is a syntax error (D) last letter
Ans:A
Q.44 In which case is it mandatory to provide a destructor in a class?
(A) Almost in every class
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
(B) Class for which two or more than two objects will be created
(C) Class for which copy constructor is defined
(D) Class whose objects will be created dynamically
Ans:D
Q.45 The members of a class, by default, are
(A) public (B) protected
(C) private (D) mandatory to specify
Ans:C
Q.46 Given a class named Book, which of the following is not a valid constructor?
(A) Book ( ) { } (B) Book ( Book b) { }
(C) Book ( Book &b) { } (D) Book (char* author, char* title) { }
Ans:B
Q47 Which of the statements is true in a protected derivation of a derived class from a base class?
(A) Private members of the base class become protected members of the derived class
(B) Protected members of the base class become public members of the derived class
(C) Public members of the base class become protected members of the derived class
(D) Protected derivation does not affect private and protected members of the derived class.
Ans:C
Q48 Which of the following statements is NOT valid about operator overloading?
(A) Only existing operators can be overloaded.
(B) Overloaded operator must have at least one operand of its class type.
(C) The overloaded operators follow the syntax rules of the original operator.
(D) none of the above.
Ans:D
Q.49 Exception handling is targeted at
(A) Run-time error (B) Compile time error
(C) Logical error (D) All of the above.
Ans:A
Q.50 A pointer to the base class can hold address of
(A) only base class object
(B) only derived class object
(C) base class object as well as derived class object
(D) None of the above
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
Ans:C
Q.51 Function templates can accept
(A) any type of parameters
(B) only one parameter
(C) only parameters of the basic type
(D) only parameters of the derived type
Ans:C
Q.52 How many constructors can a class have?
(A) 0 (B) 1
(C) 2 (D) any number
Ans:D
Q.53 The new operator
(A) returns a pointer to the variable
(B) creates a variable called new
(C) obtains memory for a new variable
(D) tells how much memory is available
Ans:C
Q.54 Consider the following statements:
int x = 22,y=15;
x = (x>y) ? (x+y) : (x-y);
What will be the value of x after executing these statements?
(A) 22 (B) 37
(C) 7 (D) Error. Cannot be executed
Ans:B
Q.55 An exception is caused by
(A) a hardware problem (B) a problem in the operating system
(C) a syntax error (D) a run-time error
Ans:D
Q.56 A template class
(A) is designed to be stored in different containers
(B) works with different data types
(C) generates objects which must be identical
(D) generates classes with different numbers of member functions.
Ans:B
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
Q.57 Which of the following is the valid class declaration header for the derived class d with base classes b1 and b2?
(A) class d : public b1, public b2 (B) class d : class b1, class b2
(C) class d : public b1, b2 (D) class d : b1, b2
Ans:A
Q.58 A library function exit() causes an exit from
(A) the loop in which it occurs (B) the block in which it occurs
(C) the function in which it occurs (D) the program in which it occurs
Ans:D
Q.59 RunTime polymorphism is achieved by ___________
(A) friend function (B) virtual function
(C) operator overloading (D) function overloading
Ans:B
Q.60 Declaration of a pointer reserves memory space
(A) for the object.
(B) for the pointer.
(C) both for the object and the pointer.
(D) none of these.
Ans:B
Q.61 An array element is accessed using
(A) a FIFO approach (B) an index number
(C) the operator (D) a member name
Ans:B
Q.62 If there is a pointer p to object of a base class and it contains the address of an object of a derived class and both classes contain a virtual member function abc(), then the statement
p->abc(); will cause the version of abc() in the __________class to be executed.
(A) Base Class (B) Derived class
(C) Produces compile time error (D) produces runtime error
Ans:B
Q.63 A pure virtual function is a virtual function that
(A) has no body (B) returns nothing
(C) is used in base class (D) both (A) and (C)
Ans:D
Q.64 A static function
(A) should be called when an object is destroyed.
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
(B) is closely connected with and individual object of a class.
(C) can be called using the class name and function name.
(D) is used when a dummy object must be created.
Ans:C
Q.65 We can output text to an object of class ostream using the insertion operator<< because
(A) the ostream class is a stream
(B) the insertion operator works with all classes.
(C) we are actually outputting to cout.
(D) the insertion operator is overloaded in ostream.
Ans:D
Q.66 The statement f1.write((char*)&obj1, sizeof(obj1));
(A) writes the member function of obj1 to f1.
(B) Writes the data in obj1 to f1.
(C) Writes the member function and the data of obj1 to f1.
(D) Writes the address of obj1 to f1.
Ans:B
Q.67 To convert from a user defined class to a basic type, you would most likely use.
(A) A built-in conversion function.
(B) A one-argument constructor.
(C) A conversion function that’s a member of the class.
(D) An overloaded ‘=‘ operator.
Ans:C
Q.68 Which of the following is not the characteristic of constructor.
(A) They should be declared in the public section.
(B) They do not have return type.
(C) They can not be inherited.
(D) They can be virtual.
Ans:D
Q.69 Name the header file to be included for the use of built in function isalnum()
(A) string.h (B) process.h
(C) ctype.h (D) dos.h
Ans:C
Q.70 What is the output of given code fragment?
int f=1, i=2;
while(++i<5)
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
f*=i;
cout<<f;
(A) 12 (B) 24
(C) 6 (D) 3
Ans:A
Q.71 A class defined within another class is:
(A) Nested class (B) Inheritance
(C) Containership (D) Encapsulation
Ans:A
Q.72 What will be the values of x, m and n after the execution of the following statements?
int x, m, n;
m = 10;
n = 15;
x = ++m + n++;
(A) x=25, m=10, n=15 (B) x=26, m=11, n=16
(C) x=27, m=11, n=16 (D) x=27, m=10, n=15
Ans:B
Q.73 Which of the following will produce a value 10 if x = 9.7?
(A) floor(x) (B) abs(x)
(C) log(x) (D) ceil(x)
Ans:D
Q74 The major goal of inheritance in c++ is:
(A) To facilitate the conversion of data types.
(B) To help modular programming.
(C) To extend the capabilities of a class.
(D) To hide the details of base class.
Ans:C
Q.75 Consider the following class definitions:
class a
{
};
class b: protected a
{
};
What happens when we try to compile this class?
(A) Will not compile because class body of a is not defined.
(B) Will not compile because class body of b is not defined.
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
(C) Will not compile because class a is not public inherited.
(D) Will compile successfully.
Ans:D
Q.76 Which of the following expressions is illegal?
(A)
. (B)
(false && true)
(C) bool (x) = (bool)10; (D) float y = 12.67;
Ans:C
Q.77 The actual source code for implementing a template function is created when
(A) The declaration of function appears.
(B) The function is invoked.
(C) The definition of the function appears.
(D) None of the above.
Ans:B
Q.78 An exception is caused by
(A) a runtime error.
(B) a syntax error.
(C) a problem in the operating system.
(D)a hardware problem.
Ans:A
Q.79 Which of the following statements are true in c++?
(A) Classes can not have data as public members.
(B) Structures can not have functions as members.
(C) Class members are public by default.
(D)None of these.
Ans:B
Q.80 What would be the output of the following program?
int main()
{
int x,y=10,z=10;
x = (y = =z);
cout<<x;
return 0;
}
(A) 1 (B) 0
(C) 10 (D) Error
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
Ans:A
Q.81 What is the error in the following code?
class t
{
virtual void print();
}
(A) No error.
(B) Function print() should be declared as static.
(C) Function print() should be defined.
(D) Class t should contain data members.
Ans:A
Q.82 What will be the output of following program?
#include<iostream.h>
void main()
{
float x;
x=(float)9/2;
cout<<x;
}
(A) 4.5 (B) 4.0
(C) 4 (D) 5
Ans:A
Q.83 A white space is :
(A) blank space (B) new line
(C) tab (D) all of the above
Ans:D
Q.84 The following can be declared as friend in a class
(A) an object (B) a class
(C) a public data member (D) a private data member
Ans:B
Q.85 What would be the output of the following?
#include<iostream.h>
void main()
{
char *ptr=“abcd”
char ch;
ch = ++*ptr++;
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
cout<<ch;
}
(A) a (B) b
(C) c (D) d
Ans:B
Q.86 A copy constructor takes
(A) no argument (B) one argument
(C) two arguments (D) arbitrary no. of arguments
Ans:B
Q87 Overloading a postfix increment operator by means of a member function takes
(A) no argument (B) one argument
(C) two arguments (D) three arguments
Ans:A
Q88 Which of the following ways are legal to access a class data member using this pointer?
(A) this.x (B) *this.x
(C) *(this.x) (D) (*this).x
Ans:D
Q.89 If we store the address of a derived class object into a variable whose type is a pointer to the base class, then the object, when accessed using this pointer.
(A) continues to act like a derived class object.
(B) Continues to act like a derived class object if virtual functions are called.
(C) Acts like a base class object.
(D) Acts like a base class, if virtual functions are called.
Ans:B
Q.90 Which of the following declarations are illegal?
(A) void *ptr; (B) char *str = “hello”;
(C) char str = “hello”; (D) const *int p1;
Ans:C
Q.91 What will be the result of the expression 13 & 25?
(A) 38 (B) 25
(C) 9 (D) 12
Ans:C
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
Q.92 Which of the following operator can be overloaded through friend function?
(A) ->
(B) =
(C) ( )
(D) *
Ans:D
Q.93 To access the public function fbase() in the base class, a statement in a derived class function fder() uses the statement.fbase();
(A) fbase();
(B) fder();
(C) base::fbase();
(D) der::fder();
Ans:A
Q.94 If a base class destructor is not virtual, then
(A) It can not have a function body.
(B) It can not be called.
(C) It can not be called when accessed from pointer.
(D) Destructor in derived class can not be called when accessed through a pointer to the base class.
Ans:D
Q.95 Maximum number of template arguments in a function template is
(A) one
(B) two
(C) three
(D) many
Ans:D
Q 96 In access control in a protected derivation, visibility modes will change as follows:
(A) private, public and protected become protected
(B) only public becomes protected.
(C) public and protected become protected.
(D) only private becomes protected.
Ans:C
Q 97 Which of the following statement is valid?
(A) We can create new C++ operators.
(B) We can change the precedence of the C++ operators.
(C) We can change the associativity of the C++ operators.
(D) We can not change operator templates.
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
Ans:D
Q.98 What will be the output of the following program?
#include<iostream.h>
void main()
{
float x=5,y=2;
int result;
result=x % y;
cout<<result;
}
(A) 1 (B) 1.0
(C) Error message (D) 2.5
Ans:C
Q.99 Which can be passed as an argument to a function?
(A) constant (B) expression
(C) another function (D) all of the above.
Ans:A
Q.100 Member functions, when defined within the class specification:
(A) are always inline.
(B) are not inline.
(C) are inline by default, unless they are too big or too complicated.
(D) are not inline by default.
Ans:A
Code: AC-11 Subject:OBJECT ORIENTED PROGRAMMING PART – II, VOL – I
DESCRIPTIVES
Q.1 Explain the following. (16)
(i) Conversion from Class to Basic Type.
(ii) File Pointers.
(iii) Function Prototyping.
(iv) Overload resolution.
Ans:(i) Conversion from Class to Basic Type: We know that conversion from a basic to class type can be easily done with the help of constructors. The conversion from class to basic type is indeed not that easy as it cannot be done using constructors. for this conversion we need to define an overloaded casting operator. This is usually referred to as a conversion function. The syntax for this is as follows:
The above function shall convert a class type data to typename.
A conversion function must follow the following 3 rules:
a. It cannot have a return type
b. It has to be declared inside a class.
c. It cannot have any arguments.
(ii)File pointers: we need to have file pointers viz. input pointer and output pointer. File pointers are required in order to navigate through the file while reading or writing. There are certain default actions of the input and the output pointer. When we open a file in read only mode, the input pointer is by default set at the beginning. When we open a file in write only mode, the existing contents are deleted and the file pointer is attached in the beginning.
C++ also provides us with the facility to control the file pointer by ourselves. For this, the following functions are supported by stream classes: seekg(), seekp(), tellg(), tellp().
(iii) Function prototyping is used to describe the details of the function. It tells the compiler about the number of arguments, the type of arguments, and the type of the return values. It some what provides the compiler with a template that is used when declaring and defining a function. When a function is invoked, the compiler carries out the matching process in which it matches the declaration of the function with the arguments passed and the return type. In C++, function prototype was made compulsory but ANSI C makes it optional. The syntax is as follows:
type function-name(argument-list);
example int func(int a, int b, int c);
(iv)Overload resolution: When we overload a function, the function prototypes are matched by the compiler. The best match is found using the following steps.
a. The compiler first matches that prototype which has the exactly same types of actual arguments.
b. If an exact match is not found by the first resolution, the integral promotions to the actual arguments are used like char to int, float to double.
c. When both the above methods fail, the built in conversions to the actual arguments are used like long square (long n).
d. If all the above steps do not help the compiler find a unique match, then the compiler uses the user defined convergence, in combination with integral promotions and built in conversions.
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
Q.2 Write a C++ program that prints (6)
|
1 |
1.0000e+00 |
1.000 |
|
2 |
5.0000e-01 |
1.500 |
|
3 |
3.3333e-01 |
1.833 |
Ans: #include<iostream.h>
#include<iomanip.h>
void main()
{
float x,y,z;
x=1.0000e+00;
y=5.0000e-01;
z=3.3333e-01;
cout.setf(ios::showpoint);
cout.precision(3);
cout.setf(ios::fixed,ios::floatfield);
cout<<x<<'\n';
cout<<y<<'\n';
cout<<z<<'\n';
}
Q.3 What are friend functions? Explain there characteristics with a suitable example. (6)
Ans: We generally have a belief that a non member function cannot access the private data member of the class. But this is made possible using a friend function. A friend function is that which does not belong to the class still has complete access to the private data members of the class. We simply need to declare the function inside the class using the keyword “friend”. The syntax for declaring the friend function is as follows:
class demo
{
public:
friend void func (void);
}
The characteristics of a friend function are as follows.
· It can be declared both in the private and the public part of the class without altering the meaning.
· It is not called using the object of the class.
· To access the data members of the class, it needs to use the object name, the dot operator and the data member’s name. example obj.xyz
· It is invoked like a normal function.
· It does not belong to the class
An example program is as follows:
#include<iostream.h>
using namespace std;
class one;
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
class two
{
int a;
public:
void setvalue(int n){a=n;}
friend void max(two,one);
};
class one
{
int b;
public:
void setvalue(int n){b=n;}
friend void max(two,one);
};
void max(two s,one t)
{
if(s.a>=t.b)
cout<<s.a;
else
cout<<t.b;
}
int main()
{
one obj1;
obj1.setvalue(5);
two obj2;
obj2.setvalue(10);
max(obj2,obj1);
return 0;
}
Q.4 write a program to overload the operator ‘+’ for complex numbers. (7)
Ans:
To perform complex number addition using operator overloading.
# include <iostream.h>
# include <conio.h>
class complex {
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
float r, i;
public:
complex()
{
r=i=0;
}
void getdata()
{
cout<<”R.P”;
cin>>r;
cout<<”I.P”;
cin>>i;
}
void outdata (char*msg)
{
cout<<endl<,msg;
cout<<”(“<<r;
cout<<”+j” <<i<<”)”;
}
Complex operator+(Complex);
};
Complex complex::Operator+(Complex(2))
{
complex temp;
temp.r=r+c2.r;
temp.i=I=c2.i;
return(temp);
}
void main()
{
clrscr();
complex c1, c2, c3;
cout<<”Enter 2 complex no: “<<endl;
cl.getdta();
c2.getdata();
c3=c1+c2;
c3.outdata (“The result is :”);
getch();
}
OUTPUT
Enter 2 complex no: R.P: 2 I.P: 2 R.p: 2 I.P:2
The result is: 4+j4
RESULT
Thus the complex number addition has been done using operator overloading
Q.5 Write a complete C++ program to do the following :
(i) ‘Student’ is a base class, having two data members: entryno and name;
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
entryno is integer and name of 20 characters long. The value of
entryno is 1 for Science student and 2 for Arts student, otherwise it is
an error.
(ii) ‘Science’ and ‘Arts’ are two derived classes, having respectively data
items marks for Physics, Chemistry, Mathematics and marks for
English, History, Economics.
(iii) Read appropriate data from the screen for 3 science and 2 arts
students.
(iv) Display entryno, name, marks for science students first and then for
arts students. (14)
Ans:
#include<iostream.h>
class student
{
protected:
int entryno;
char name[20];
public:
void getdata()
{
cout<<"enter name of the student"<<endl;
cin>>name;
}
void display()
{
cout<<"Name of the student is"<<name<<endl;
}
};
class science:public student
{
int pcm[3];
public:
void getdata()
{
student::getdata();
cout<<"Enter marks for Physics,Chemistry and Mathematics"<<endl;
for(int j=0;j<3;j++)
{
cin>>pcm[j];
}
}
void display()
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
{
entryno=1;
cout<<"entry no for Science student is"<<entryno<<endl;
student::display();
cout<<"Marks in Physics,Chemistry and Mathematics are"<<endl;
for(int j=0;j<3;j++)
{
cout<<pcm[j]<<endl;;
}
}
};
class arts:public student
{
int ehe[3];
public:
void getdata()
{
student::getdata();
cout<<"Enter marks for English,History and Economics"<<endl;
for(int j=0;j<3;j++)
{
cin>>ehe[j];
}
}
void display()
{
entryno=2;
cout<<"entry no for Arts student is"<<entryno<<endl;;
student::display();
cout<<"Marks in English,History and Economics are"<<endl;
for(int j=0;j<3;j++)
{
cout<<ehe[j]<<endl;;
}
}
};
void main()
{
science s1[3];
arts a1[3];
int i,j,k,l;
cout<<"Entry for Science students"<<endl;
for(i=0;i<3;i++)
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
{
s1[i].getdata();
}
cout<<"Details of three Science students are"<<endl;
for(j=0;j<3;j++)
{
s1[j].display();
}
cout<<"Entry for Arts students"<<endl;
for(k=0;k<3;k++)
{
a1[k].getdata();
}
cout<<"Details of three Arts students are"<<endl;
for(l=0;l<3;l++)
{
a1[l].display();
}
}
Q.6 An electricity board charges the following rates to domestic users to discourage large consumption of energy :
For the first 100 units - 50 P per unit
Beyond 300 units - 60 P per unit
If the total cost is more than Rs.250.00 then an additional surcharge of
15% is added on the difference. Define a class Electricity in which the
function Bill computes the cost. Define a derived class More_Electricity
and override Bill to add the surcharge. (8)
Ans:
#include<iostream.h>
class electricity
{
protected:
float unit;
float cost;
public:
void bill()
{
cout<<"\n enter the no. of units"<<endl;
cin>>unit;
if(unit<=100)
{
cost=0.50*unit;
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
cout<<"cost up to 100 unit is Rs."<<cost<<endl;
}
else
{
if(unit>300)
{
cost=0.60*unit;
cout<<"Beyond 300 units is Rs"<<cost;
}
}
}
};
class more_electricity:public electricity
{
float surcharge,diff,total_cost;
public:
void bill()
{
electricity::bill();
if(cost>250.00)
{
diff=cost-250;
surcharge=diff*0.15;
total_cost=cost+surcharge;
cout<<" Bill amount with surcharge is Rs"<<total_cost;
}
else
{
cout<<"Bill amout is Rs."<<cost;
}}};
void main()
{
more_electricity me;
me.bill();
}
Q.7 Write a program to open a file in C++ “Hello.dat” and write
“This is only a test”
“Nothing can go wrong”
“All things are fine…”
into the file. Read the file and display the contents. (6)
Ans:
#include<iostream.h>
#include<fstream.h>
void main()
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
{
ofstream fout;
fout.open("Hello.dat");
fout<<"This is only a test\n";
fout<<"Nothing can go wrong\n";
fout<<"All things are fine....\n";
fout.close();
const int N=100;
char line[N];
ifstream fin;
fin.open("Hello.dat");
cout<<"Contents of the Hello.dat file\n";
while(fin)
{
fin.getline(line,N);
cout<<line;
}
fin.close();}
Q.8 Develop a program in C++ to create a database of the following items of the derived class.
Name of the patient, sex, age, ward number, bed number, nature of illness, date of admission.
Design a base class consisting of data members : name of the patient, sex and age ; and another base class consisting of the data members : bed number and nature of the illness. The derived class consists of the data member ,date of admission.
Program should carry out the following methods
(i) Add a new entry.
(ii) List the complete record. (10)
Ans:. #include<conio.h>
#include<iostream.h>
class A{
public:
char name[20];
char sex[10];
int age;
void get_data();
void disp_data();
};
void A::get_data(){
cout<<"enter d name:";
cin>>name;
cout<<"enter d age:";
cin>>age;
cout<<"enter d sex:";
cin>>sex;}
void A::disp_data(){
cout<<"name:"<<name<<'\n';
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
cout<<"age:"<<age<<'\n';
cout<<"sex:"<<sex<<'\n';}
class B{
public:
int bed_number;
char nature_illness[40];
void get_data();
void disp_data();
};
void B::get_data(){
cout<<"enter d bed_number:";
cin>>bed_number;
cout<<"enter d nature_illness:";
cin>>nature_illness;
}
void B::disp_data(){
cout<<"bed_number:"<<bed_number<<'\n';
cout<<"nature_illness:"<<nature_illness<<'\n';
}
class C:public A,public B{
int date_admission;
public:
void get_data();
void disp_data();
void record();
};
void C::get_data(){
A::get_data();
B::get_data();
cout<<endl<<"Enter Data of Admission:-> ";
cin>>date_admission;
}
void C::disp_data(){
A::disp_data();
B::disp_data();
cout<<endl<<"Date of Admission\t"<<date_admission;
}
void main(){
clrscr(); C c1;
cout<<endl<<"Adding a new record to database\n";
getch();
c1.get_data();
cout<<endl<<"Displaying the added record to database\n";
c1.disp_data();
getch();
}
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
Q.9 How many times is the copy constructor called in the following code? (4)
Apple func(Apple u)
{
Apple w=v;
Return w;
}
void main()
{
Apple x;
Apple y = func (x);
Apple z = func (y);
}
Ans: 2 times
Q.10 Write a program code which throws an exception of type char* and another of type int. Write a try ---- catch block which can catch both the exception. (5)
Ans:#include <iostream>
using namespace std;
int main()
{
double Operand1, Operand2, Result;
// Request two numbers from the user
cout << "This program allows you to perform a division of two numbers\n";
cout << "To proceed, enter two numbers: ";
try {
cout << "First Number: ";
cin >> Operand1;
cout << "Second Number: ";
cin >> Operand2;
// Find out if the denominator is 0
if( Operand2 == 0 )
throw "Division by zero not allowed";
// Perform a division and display the result
Result = Operand1 / Operand2;
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
cout << "\n" << Operand1 << " / " << Operand2 << " = " << Result << "\n\n";
}
catch(const char* Str) // Catch an exception
{
// Display a string message accordingly
cout << "\nBad Operator: " << Str;
}
return 0;
#include <iostream.h>
int main()
{
double Operand1, Operand2, Result;
const char Operator = '/';
// Request two numbers from the user
cout << "This program allows you to perform a division of two numbers\n";
cout << "To proceed, enter two numbers\n";
try {
cout << "First Number: ";
cin >> Operand1;
cout << "Second Number: ";
cin >> Operand2;
// Find out if the denominator is 0 if( Operand2 == 0 ) throw 0;
// Perform a division and display the result
Result = Operand1 / Operand2;
cout << "\n" << Operand1 << " / " << Operand2 << " = " << Result << "\n\n";
}
catch(const int n) // Catch an exception
{
// Display a string message accordingly
cout << "\nBad Operator: Division by " << n << " not allowed\n\n";
}
return 0;
}
Q.11 Create a class Time that has separate int member data for hours, minutes and seconds. One constructor should initialize this data to zero and another constructor initialize it to fixed values. Write member function to display time in 12 hour as well as 24 hour format. The final member function should add two objects of class Time.
A main() program should create three objects of class time, of which two are initialized to specific values and third object initialized to zero. Then it should add the two
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
initialized values together, leaving the result in the third. Finally it should display the value of all three objects with appropriate headings. (14)
Ans:
#include<iostream.h>
#include<string.h>
class time24
{
int hours,minutes,seconds;
public:
time24()
{
hours=minutes=seconds=0;
}
time24(int h,int m,int s)
{
hours=h;
minutes=m;
seconds=s;
}
void display()
{
if(hours<10)
cout<<'0';
cout<<hours<<":";
if(minutes<10)
cout<<'0';
cout<<minutes<<":";
if(seconds<10)
cout<<'0';
cout<<seconds;
}
};
class time12
{
bool pm;
int hour,minute;
public:
time12()
{
pm=true;
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
hour=minute=0;
}
time12(bool ap,int h,int m)
{
pm=ap;
hour=h;
minute=m;
}
time12(time 24);
void display()
{
cout<<hour<<":";
if(minute<10)
cout<<'0';
cout<<minute<<" ";
char *am_pm=pm ? "p.m." : "a.m.";
cout<<am_pm;
}
};
time12::time12(time24 t24)
{
int hrs24=hours;
bool pm=hours<12 ? false:true;
int min=seconds<30 ? minutes:minutes+1;
if(min==60)
{
min=0;
++hrs24;
if(hrs24==12 || hrs24==24)
pm=(pm==true)? false:true;
}
int hrs12=(hrs24<13) ? hrs24 : hrs24-12;
if(hrs12==0)
{
hrs12=12;
pm=false;
}
return time12(pm,hrs12,min);
}
int main()
{
int h1,m1,s1;
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
while(true)
{
cout<<"enter 24-hour time:\n";
cout<<"Hours(0-23):";
cin>>h1;
if(h1>23)
return(1);
cout<<"Minutes:";
cin>>m1;
cout<<"Seconds:";
cin>>s1;
time24 t24(h1,m1,s1);
cout<<"you entered:";
t24.display();
cout<<endl;
time12 t12=t24;
cout<<"\n12-hour time:";
t12.display();
cout<<endl;
}
return 0;
}
Q.12 In the following output, the following flag constants are used with the stream member function setf. What effect does each have
(i) ios::fixed
(ii) ios::scientific
(iii) ios::showpoint
(iv) ios::showpos
(v) ios::right
(vi) ios::left (6)
Ans: A two argument version of setf() is used to set flag which is a member of a group of flags. The second argument specifies the group and is a bitwise OR of all the flags in the group. The setfO, a member function of the ios class, can provide answers to these and other formatting questions. The setfO (setf stands for set flags) function can be us follows:
cout.setf(argl,arg2)
The argl is one of the formatting flags defined in the class ios. The formatting flag Spi the format action required for the output. Another ios constant, arg2, known as bi specifies the group to which the formatting flag belongs.
There are three bit and each has a group of format flags which are mutually exclusive Examples:
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
cout.setf(ios::1eft, ios::adjustfied);
cout.setf(ios::scientific, ios::floatfield);
Note that the first argument should be one of the group members of the second argument.
The Setf() can be used with the flag ios::showpoint as a single argument to achieve this form of output. For example
Cout.setf(ios::showpoint);
Would cause cout to display trailing zeros and trailing decimal point.
Similarly, a plus sign can be printed a positive number using the following statement:
Cout.setf(ios::showpos);set to selection
selection ios::ingroup Meaning ----------------------------------------------------------- ios::left ios::adjustfield left alignment ios::right ios::adjustfield right alignmentios::fixed ios::floatfield fixed form of floating point
ios::scientific ios::floatfield force exponential format
ios::showpoint // Always show a point
ios::showpos // If positive still show sign
Q.13 Define a class Distance with feet and inch and with a print function to print the distance. Write a non-member function max which returns the larger of two distance objects, which are arguments. Write a main program that accepts two distance objects from the user, compare them and display the larger. (8)
Ans:
#include<iostream.h>
class distance
{
int feet;
float inches;
public:
distance()
{
feet=0;
inches=0.0;
}
void getdist()
{
cout<<"Enter feet:"<<endl;
cin>>feet;
cout<<"Enter inches:"<<endl;
cin>>inches;
}
void showdist()
{
cout<<feet<<endl<<inches<<endl;
}
friend void maxdist(distance d1, distance d2)
{
d1.feet=d1.feet+ d1.inches/12;
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
d2.feet=d2.feet+ d2.inches/12;
if(d1.feet>d2.feet)
{
cout<<"first distance is greater";
}
else
{
cout<<"second distance is greater";
}
}
};
void main()
{
distance d3,d4;
d3.getdist();
d3.showdist();
d4.getdist();
d4.showdist();
maxdist(d3,d4);
}
Q.14 a) Define the class Person which has name (char*) and age (int). Define the following constructors
a. default
b. with name as argument (assume age is 18)
c. with name and age as argument
Also define the copy constructor. (7)
Ans:
#include<iostream.h>
#include<string.h>
class person
{
char *name;
int age;
public:
person()
{
name=NULL;
//strcpy(name,NULL);
age=0;
}
person(char *n)
{
strcpy(name,n);
age=18;
}
person(char *n1,int a)
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
{
strcpy(name,n1);
age=a;
}
person(person &p)
{
strcpy(name,p.name);
age=p.age;
}
void disp()
{
cout<<"name is"<<name;
cout<<"age is"<<age;
}
};
void main()
{
person p1("ram"),p2("sita",20),p3=p2;
cout<<"hello";
p1.disp();
p2.disp();
p3.disp();
}
Q.14 b).Using the class above, define two subclasses Student and Professor. Student subclass displays the name and CGPA (grade points in float) and Professor subclass displays the name and number of publications (int). Write a main program using polymorphism to display the data of one student and one professor. (7)
Ans:
#include<iostream.h>
class person
{
protected:
char name[40];
public:
void getname()
{
cout<<"Enter name:";
cin>>name;
}
void disp()
{
cout<<"Name is:"<<name<<endl;
}
virtual void getdata()=0;
virtual bool outstanding()=0;
};
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
class student:public person
{
float gpa;
public:
void getdata()
{
person::getname();
cout<<"Enter student's GPA:";
cin>>gpa;
}
bool outstanding()
{
return(gpa>3.5) ? true:false;
}
};
class professor:public person
{
int numpubs;
public:
void getdata()
{
person::getname();
cout<<"enter number of professor's publication:";
cin>>numpubs;
}
bool outstanding()
{
return(numpubs>100) ? true:false;
}
};
void main()
{
person *perptr[10];
int n=0;
char choice;
do
{
cout<<" Enter student or professor(s/p):";
cin>>choice;
if(choice=='s')
perptr[n]=new student;
else
perptr[n]=new professor;
perptr[n++]->getdata();
cout<<"enter another(y/n)?";
cin>>choice;
}while (choice=='y');
for(int j=0;j<n;j++)
{
perptr[j]->disp();
if(perptr[j]->outstanding())
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
cout<<"this person is outstanding\n";
}
}
Q.15 Write a global function which uses the above operator and prints the number and the number of times it appears (frequency) .
Ans:
void frequency_calc()
{
bag b;
for(int j=0;j<50;j++)
{
int temp;
cin>>temp;
b[j]=temp;
}
for(int i=0;i<50;i++)
{
int k=0;
for(j=j+1;j<50;j++)
{
if (b[i]==b[j])
k++;
}
cout<<b[i]<<”occurred”<<k<<”times”;
}
}
Q.16 How are template functions overloaded? Explain with a suitable example. (8)
Ans: A template function can be overloaded using a template functions. It can also be overloaded using ordinary functions of its name. The matching of the overloaded function is done in the following manner.
a. The ordinary function that has an exact match is called.
b. A template function that could be created with an exact match is called.
c. The normal matching process for overloaded ordinary functions is followed and the one that matches is called.
If no match is found, an error message is generated. Automatic conversion facility is not available for the arguments on the template functions. The following example illustrates an overloaded template function:
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
#include<iostream.h>
#include<string.h>
using namespace std;
template<class T>
void display(T x)
{
cout<<"Template display: "<<x<<"\n";
}
void display(int x)
{
cout<<"Explicit display: "<<x<<"\n";
}
int main()
{
display(20);
display(3.6);
display('A');
return 0;
}
Q.17 Give a function template SEARCH that can be used to search an array of elements of any type and returns the index of the element, if found. Give both the function prototype and the function definition for the template. Assume a class template Array <T> is available. (5)
Ans:
#include<iostream.h>
template<class atype>
//function returns index number of item, or -1 if not found
int find(atype *array,atype value,int size)
{
for(int j=0;j<size;j++)
if(array[j]==value)
return j;
return -1;
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
}
char chrarr[]={1,3,5,9,11,13};
char ch=5;
int intarr[]={1,3,5,9,11,13};
int in=6;
long lonarr[]={1L,3L,5L,9L,11L,13L};
long lo=11L;
double dubarr[]={1.0,3.0,5.0,9.0,11.0,13.0};
double db=4.0;
int main()
{
cout<<"\n 5 in chrarr: index="<<find(chrarr,ch,6);
cout<<"\n 6 in intarr: index="<<find(intarr,in,6);
cout<<"\n 11 in lonarr: index="<<find(lonarr,lo,6);
cout<<"\n 4 in dubarr: index="<<find(dubarr,db,6);
cout<<endl;
return 0;
}
Q.18 Answer briefly :
(i) What happens in a while loop if the control condition is false initially ?
(ii) What is wrong with the following loop
while (n <= 100)
Sum += n*n;
(iii) What is wrong with the following program
int main( )
{
const double PI;
int n;
PI = 3.14159265358979;
n = 22;
}
(iv) When a multidimensional array is passed to a function, why does C++ require all but the first dimension to be specified in parameter list?
(v) Why can’t ** be overloaded as an exponentiation operator?
(vi) How many constructors can a class have, and how should they be distinguished.
(2x6=12)
Ans:
i) If the condition in while loop initially false the loop does not enter into the statements inside the loop and terminates initially.
ii) In the given statement of while loop n is not initialized. Although there is no initialization expression, the loop variable n must be initialized before the loop begins.
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
iii) The given program code is wrong as const declared PI must be initialized at the time of declaration. Whereas initialization of int n is correct the correct code is
Int main()
{Const double PI=3.1459265358979;
Int n=22;}
iv) Multidimensional array is an array of arrays the function first take the argument as an array of parameter first. It does not need to know how many values in parameter there are, but it does need to know how big each parameter element is, so it can calculate where particular element is. So we must tell it the size of each element but not how many there are.
v) Only existing operator symbols may be overloaded. New symbols, such as ** for exponentiation, cannot be defined.
vi) A class can have Default constructor, constructor with an argument, constructor with two argument and copy constructor.
Q.19 What are the various ways of handling exceptions? Which one is the best? Explain. (6)
Ans: Exception handling is a mechanism that finds errors at run time. It detects an error and reports so that an appropriate action can be taken. It adopts the following ways:
a. Hit the exception
b. Throw the exception
c. Catch the exception
d. Handle the exception
The three blocks that is used in exception handling are try throw and catch. A further advancement that is made is of multiple catch handlers.
A program may have more than one condition to throw an exception. For this we can use more than one catch block in a program. An example is shown below illustrate the concept:
#include<iostream.h>
using namespace std;
void test(int x){
try {
if(x==1) throw x;
else
if(x==0) throw 'x';
else
if(x==-1) throw 1.0;
cout<<"End of try-block\n";
}
catch(char c) {
cout<<"Caught a character \n";
}
catch(int m) {
cout<<"Caught an integer\n";
}
catch(double d) {
cout<<"Caught a double\n";
}
cout<<""End of try-catch system\n\n;}
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
int main(){
cout<<"Testing multiple catches\n";
cout<<"x==1\n";
test(1);
cout<<"x==0\n";
test(0);
cout<<"x==-1\n";
test(-1);
cout<<"x==2\n";
test(2); return 0;}
Q.20 Describe, in brief, the steps involved in object oriented analysis. (6)
Ans: The steps involved in object oriented analysis.
Object-oriented analysis provides us with a simple, yet powerful, mechanism for identifying objects, the building block of the software to be developed. The analysis is basically concerned with the decomposition of a problem into its component parts and establishing a logical model to describe the system functions.
The object-oriented analysis (OOA) approach consists of the following steps:
1. Understanding the problem.
2. Drawing the specifications of requirements of the user and the software.
3. Identifying the objects and their attributes.
4. Identifying the services that each object is expected to provide (interface).
5. Establishing inter-connections (collaborations) between the objects in terms of services required and services rendered.
|
Requirement specification Identify services Identify collaborations Identify object
![]()
![]()
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
Problem Understanding
The first step in the analysis process is to understand the problem of the user. The problem statement should be refined and redefined in terms of computer system engineering that could suggest a computer-based solution. The problem statement should be stated, as far as possible, in a single, grammatically correct sentence. This will enable the software engineers to have a highly focused attention on the solution of the problem. The problem statement provides the basis for drawing the requirements specification of both the user and the software.
Requirements Specification
Once the problem is clearly defined, the next step is to understand what the proposed. System is required to do. It is important at this stage to generate a list of user requirement A clear understanding should exist between the user and the developer of what is require Based on the user requirements, the specifications for the software should be drawn. Developer should state clearly
What outputs are required?
What processes are involved to produce these outputs?
What inputs are necessary?
What resources are required?
These specifications often serve as a reference to test the final product for its performance the intended tasks.
Identification of Objects
Objects can often be identified in terms of the real-world objects as well as the abstra~
objects. Therefore, the best place to look for objects is the application itself. The application
may be analyzed by using one of the following two approaches:
1.Data flow diagrams (DFD)
2.Textual analysis (TA)
Identification of Services
Once the objects in the solution space have been identified, the next step is to identify set services that each object should offer. Services are identified by examining all the verbs are verb phrases in the problem description statement. Doing verbs and compare verbs usually give rise to services (which we call as functions C++). Being verbs indicate the existence of the classification structure while having verbs' rise to the composition structures.
Establishing interconnection
This step identifies the services that objects provide and receive. We may use an information flow diagram(IED) or an entity-relationship (ER) diagram to enlist this information.
Q.21 Write a program in C++ which calculates the factorial of a given number. (6)
Ans: Program to calculate factorial of a given number
#include <iostream.h>
long factorial (long a){ if (a > 1) return (a * factorial (a-1)); else return (1);
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
} int main (){ long ln; cout << "Type a number: "; cin >> ln; cout << "!" << ln << " = " << factorial (ln); return 0;}
Q.22 Define a class Queue with the following data members
(i) An array of numbers that the Queue can hold
(ii) Indices ‘front’ and ‘rear’ to specify the positions in Queue.
Initially set front = rear = -1
and the following member functions
(i) Delete – Remove front element.
(ii) Add – Add an element at rear. (8)
Ans:
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct Node{
int data;
Node *next;
}*rear,*front;
void Create();
void AddItem();
void DeleteItem();
void Display();
void main(){
int ch=0;
Create();
clrscr();
while(ch!=4){
cout<<"\n1. Add item";
cout<<"\n2. Delete item";
cout<<"\n3. Display";
cout<<"\n4. Quit";
cout<<"\n Enter choice";
cin>>ch;
switch(ch){
case 1:
AddItem();
break;
case 2:
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
DeleteItem();
break;
case 3:
Display();
break;
}
}
}
void Create(){
front=rear=NULL;
}
void AddItem(){
Node *p=new Node;
cout<<"\n Enter the data:";
cin>>p->data;
p->next=NULL;
if(front==NULL){
front=rear=p;
}
else{
rear->next=p;
rear=p;
}
}
void DeleteItem(){
if(front==NULL){
cout<<"\nQueue is empty";
return;
}
if(front==rear){
cout<<endl<<front->data<<" is deleted";
delete front;
front=rear=NULL;
}
else{
Node *p=front;
cout<<endl<<front->data<<" is deleted";
front=front->next;
delete p;
}
}
void Display(){
Node *p=front;
while(p!=NULL){
cout<<"\n"<<p->data;
p=p->next;
}}
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
Q.23 What are the two methods of opening a file? Explain with examples. What is the difference between the two methods? (10)
Ans: For opening a file, we first create a file stream which we can link to the file name. we can define a file stream using 3 classes namely ifstream, ofstream and fstream that are all contained in the header file fstream. Which class to use shall depend upon the mode in which we want to open the file viz, read or write. There are 2 modes to open a file.
· Using the constructor function of the class
· Using the member function “open()” of the class.
When using the first method that is constructor function of the class, we initialize the file stream object by file name. for example the following statement opens a file named “results” for input.
ifstream infile (“results”);
When using the second method of function “open()”, multiple files can be opened that use the same stream object for example when we wish to process a number of files in sequential manner, we shall create a single stream object and use it to open each file in turn. The syntax is as follows:
file-stream-class stream-object;
stream-object.open(“filename”);
The basic difference between the two methods is that the constructor function of the class is used when we wish to open just one file in the stream. The open function is used when we wish to open multiple files using one stream.
Q.24 What is containership? How does it differ from inheritance? (4)
Ans: Inheritance, one of the major features of OOP, is to derive certain properties of the base class in the derived class. A derived class can make use of all the public data members of its base classes.
Containership is different from Inheritance. When one class is defined inside another class, it is known as containership or nesting. We can thus imply that one object can be a collection of many different objects. Both the concepts i.e. inheritance and containership have a vast difference. Creating an object that contains many objects is different from creating an independent object. First the member objects are created in the order in which their constructors occur in the body. Then the constructor of the main body is executed. An initialization list of the nested class is provided in the constructor.
Example:
class alpha{....};
class beta{....};
class gamma
{
alpha a;
beta b;
public:
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
gamma(arglist): a(arglist1), b(arglist2) {
//constructor body
}};
Q.25. How do the properties of following two derived classes differ?
(i) class X : public A{//..}
(ii) class Y : private A{//..} (5)
Ans: The properties of the following two derived class differ:-
(i) class X:public A{//..}
In this class A is publicly derived from class X. the keyword public specifies that objects of derived class are able to access public member function of the base class.
i) class Y:private A{//..}
In this class A is privately derived from class Y. the keyword private specifies that objects of derived class cannot access public member function of the base class. Since object can never access private members of a class.
Q.26 Identify and remove error from the following, if any :-
array_ptr = new int [10] [10] [10];
array_ptr = new int [20] [ ] [ ]; (2)
Ans: There is an error we cannot assign more than one block sizes to one variable.
Correct array_ptr new int[10];
Q.27 What is the task of the following code :-
cout << setw (4) << XYZ << endl (2)
Ans: The task of the given code is to display the value of XYZ after setting field width 4 (3) if XYZ=3; set causes the number or string that follows it in the stream to be printed within a field n characters wide.
Q.28 What is dynamic initialization of objects? Why is it needed?
How is it accomplished in C++? Illustrate. (8)
Ans: Dynamic initialization of objects means to provide the initial values to an object at run time.
Using dynamic initialization of objects we can provide different initialization formats of data for an object at run time. We can use various data types like int, float or char depending upon our need for the same object created. This provides the flexibility of using different format of data at run time depending upon the situation.
It can be accomplished using the concept of constructor overloading. For example:
#include<iostream.h>
class loan
{ long principle;
int year;
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
float rate;
public:
loan(){}
loan(long p,int y,float r=0.15);
loan(long p,int y,int r);
};
This class uses three overloaded constructors. The parameter values to these constructors are provided at run time. One can input in any of the form to create object of this class.
Q.29 State, with reason, whether the following statement is true or false
is equal to false (2)
Ans: The following statement is true as condition !(4<5)||(6>7) is equal to false.
Q.30 The
following macro is invoked as
. What is the output when x = 5
(3)
Ans: The output is 21. when the macro #define f(x) x*x-3 as f(x+3) when x=5
Q.31 List at least three C++ operators which cannot be overloaded. (3)
Ans: The operators ::, .*, . and ?: cannot be overloaded.
Q.32 Write a template function that swaps the values of two arguments passed to it. In main( ), use the function with integers and characters. (4)
Ans:
#include<iostream.h>
using namespace std;
template <class T>
void swap(T &a,T &b)
{
T temp=a;
a=b;
b=temp;
}
void func(int x,int y,char w,char z)
{
cout<<"x and y before swap: "<<x<<y<<"\n";
swap(x,y)
cout<<"x and y after swap: "<<x<<y<<"\n";
cout<<"w and z before swap: "<<w<<z<<"\n";
swap(w,z)
cout<<"w and z after swap: "<<w<<z<<"\n";
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
}
int main()
{
fun(10,20,’s’,’S’);
return 0;
}
Q.33 What is multiple inheritance? Discuss the syntax and rules of multiple inheritance in C++. How can you pass parameters to the constructors of base classes in multiple inheritance? Explain with suitable example. (12)
Ans: C++ provides us with a very advantageous feature of multiple inheritance in which a derived class can inherit the properties of more than one base class. The syntax for a derived class having multiple base classes is as follows:
Class D: public visibility base1, public visibility base2
{
Body of D;
}
Visibility may be either ‘public’ or ‘private’.
In case of multiple inheritance, the base classes are constructed in the order in which they appear in the declaration of the derived class. However the constructors for virtual base classes are invoked before any non-virtual base classes. If there are multiple virtual base classes, they are invoked in the order in which they are declared. An example program illustrates the statement.
#include<iostream.h>
using namespace std;
class M
{
protected:
int m;
public:
M(int x)
{
m=x;
cout<< “M initialized\n”;
}
};
class N
{
protected:
int n;
public:
N(int y)
{
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
n=y;
cout<< “N initialized\n”;
}
};
class P: public N, public M
{ int p,r;
public:
P(int a,int b,int c, int d):M(a),N(b)
{
p=c;
r=d;
cout<< “P initialized\n”;
}
};
void main()
{
P p(10,20,30,40);
}
Output of the program will be:
N initialized
M initialized
P initialized
Q.34 Write a program in C++ to sort a list of given numbers in nondecreasing order. (7)
Ans:
#include<iostream.h>
void main()
{ int *num,i,j,temp,n,flag;
num=new int[10];
cout<<"how many number to sort(limit)"<<endl;
cin>>n;
cout<<"enter numbers to sort in nondecreasing order"<<endl;
for(i=0;i<n;i++)
{
cin>>num[i];
}
for(i=0;i<n-1;i++)
{
flag=1;
for(j=0;j<(n-1-i);j++)
{
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
if(num[j]>num[j+1])
{
flag=0;
temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
if(flag)
break;
}
for(i=0;i<n;i++)
{
cout<<num[i]<<endl;
}
}
Q.35 What do you mean by static class members? Explain the characteristics of static class members with suitable examples. (8)
Ans: Static class member variables are used commonly by the entire class. It stores values. No different copies of a static variable are made for each object. It is shared by all the objects. It is just like the C static variables.
It has the following characteristics:
· On the creation of the first object of the class a static variable is always initialized by zero.
· All the objects share the single copy of a static variable.
· The scope is only within the class but its lifetime is through out the program.
An example is given below:
#include<iostream.h>
using namespace std;
class num
{
static int c;
int n;
public:
void getd(int x)
{
n =x;
c++;
}
void getc(void)
{
cout<<"count: "<<c<<"\n";
}
};
int num :: c;
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
int main()
{
num o1,o2,o3;
o1.getc();
o2.getc();
o3.getc();
o1.getd(1);
o2.getd(2);
o3.getd(3);
cout<<"After reading data"<<"\n";
o1.getc();
o2.getc();
o3.getc();
return 0;
}
Q.36 Create a class Patient that stores the patient name (a string) and the disease (a string) of the patient. From this class derive two classes : In_patient which has a data member roomrent (type float) and Out_patient which has a data member OPD_charges (float). Each of these three classes should have a nondefault constructor and a putdata() function to display its data. Write a main() program to test In_ patient and Out_patient classes by creating instances of them and then displaying the data with putdata(). (10)
Ans:
#include<iostream.h>
class patient
{
protected:
char *name,*disease;
public:
patient(char *n,char *d)
{
name=n;
disease=d;
}
void putdata()
{
cout<<"patient's name is"<<name<<endl;
cout<<"Disease is"<<disease<<endl;
}
};
class in_patient:public patient
{
float roomrent;
public:
in_patient(char *n,char *d,float rr):patient(n,d)
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
{
roomrent=rr;
}
void putdata()
{
patient::putdata();
cout<<"Room rent is"<<roomrent;
cout<<endl;
}
};
class out_patient:public patient
{
float opd_charges;
public:
out_patient(char *n,char *d,float opd):patient(n,d)
{
opd_charges=opd;
}
void putdata()
{
patient::putdata();
cout<<"OPD charges is"<<opd_charges;
}
};
void main()
{
char *nm,*dis;
float rr1,op1;
nm=new char;
dis=new char;
cout<<"Enter patient's name"<<endl;
cin>>nm;
cout<<"Enter disease"<<endl;
cin>>dis;
cout<<"Enter room rent"<<endl;
cin>>rr1;
cout<<"Enter opd charges"<<endl;
cin>>op1;
in_patient ip(nm,dis,rr1);
out_patient op(nm,dis,op1);
cout<<"Details of In_patient"<<endl;
ip.putdata();
cout<<"Details of Out_patient"<<endl;
op.putdata();
}
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
Q.37 Consider the following specifications: (8)
Structure name data type size
Name First array of characters 40
Mid array of characters 40
Last array of characters 60
Phone Area array of characters 4
Exch array of characters 4
Numb array of characters 6
Class name data type
N_rec Name Name
Phone Phone
(i) Declare structures in C++ for Name and Phone.
(ii) Declare a class N_rec with the following members.
1. Define the constructor (outside the class N_rec) that gathers the information from the user for Name and Phone.
2. Define the display_rec(outside the class N_rec) that shows the current values of the data members.
Ans:
#include<iostream.h>
#include<conio.h>
#include<string.h>
struct Name
{
char First[40], Mid[40], Last[60];
};
struct Phone
{
char Area[4], Exch[4], Numb[6];
};
class N_Rec
{
struct Name name;
struct Phone phone;
public:
N_Rec();
Display_Rec();
};
N_Rec :: N_Rec()
{
strcpy(name.First,"Shailesh");
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
strcpy(name.Mid," ");
strcpy(name.Last,"Saurabh");
strcpy(phone.Area,"011");
strcpy(phone.Exch,"64");
strcpy(phone.Numb,"608284");
}
N_Rec :: Display_Rec()
{
cout<<name.First<<" " <<name.Mid<<" "<<name.Last;
cout<<endl<<phone.Area<<" "<<phone.Exch<<" "<<phone.Numb;
};
main()
{
clrscr();
N_Rec obj1;
obj1.Display_Rec();
getch();
}
Q.38 When do we use multi catch handlers? Explain with an example. (8)
Ans: Multiple Catch handlers:
Just like we use conditions in a switch statement, we can use multi catch statements with one try block when a program has to throw an exception based on more than one condition.
The multi handlers are searched in sequence of which they occur. The first match that is found is executed.If no match is found then the program terminates.
An example program is given below:
#include<iostream.h>
using namespace std;
void test(int x)
{
try
{
if(x==1) throw x;
else
if(x==0) throw 'x';
else
if(x==-1) throw 1.0;
cout<<"End of try-block\n";
}
catch(char c)
{
cout<<"Caught a character \n";
}
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
catch(int m) {
cout<<"Caught an integer\n";
}
catch(double d) {
cout<<"Caught a double\n";
}
cout<<""End of try-catch system\n\n;
}
int main()
{
cout<<"Testing multiple catches\n";
cout<<"x==1\n";
test(1);
cout<<"x==0\n";
test(0);
cout<<"x==-1\n";
test(-1);
cout<<"x==2\n";
test(2);
return 0;}
The output:
Testing Multiple Catches
x==1
Caught an integer
End of try-catch system
x==0
Caught a character
End of try-catch system
x==-1
Caught a double
End of try-catch system
x==2
End of try-block
End of try-catch system
Q.39 Find errors in the following code: (6)
#include<iostream.h>
class A { int a1;
public: int a2;
protected : int a3; };
class B :public A
{ public:
void func( )
{ int b1, b2, b3;
b1 = a1;
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
b2 = a2;
b3 = a3; } };
class C : A
{ public:
void f( )
{ int c1, c2, c3;
c1 = a1;
c2 = a2;
c3 = a3; } };
int main( )
{ int p, q, r, i, j, k;
B O1;
C O2;
p = O1.a1;
q = O1.a2;
r = O1.a3;
i = O2.a1;
j = O2.a2;
k = O2.a3;
}
Ans:
In the given code:
· Class B cannot have access over the private variable a1 of class A as a private variable is never inherited. Thus b1=a1 cannot be used.
· Class C also cannot have access over the private member a1 of class A as C inherits class B which does not contain a1 as its inherited member. Thus c1=a1 cannot be used.
· Inside main() function, object O1 of class B cannot access a1. Thus the statement O1.a1 is not valid.
· Similarly object O2 of class C also cannot access a1. Thus statement O2.a1 is also not valid.
Inside main() function, the protected member a3 of class B is not accessible.So the statements “r = O1.a3; and k = O2.a3; “ is invalid
Q.40 Write short notes on any TWO :-
(i) Procedure oriented vs object oriented programming.
(ii) Object oriented design.
(iii) User defined data types in C++. (2 x 7)
Ans:
(i) Procedure oriented vs object oriented programming
Procedure-oriented programming basically consists of writing a list of instructions (or actions) for the computer to follow, and organizing these instructions into groups known as functions . We normally use a flowchart to organize these actions and represent the flow of control from one action to another. While we concentrate on the development of functions, very little
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
attention is given to the data that are being used by various functions. What happens to the data? How are they affected by the functions that work on them? In a multi-function program, many important data items are placed as global so that they may be accessed by all the functions. Each function may have its own local data. Global data are more vulnerable to an inadvertent change by a function. In a large program it is very difficult to identify what data is used by which function. In case we need to revise an external data structure, we also need to revise all functions that access the data. This provides an opportunity for bugs to creep in.
Another serious drawback with the procedural approach is that it does not model real world problems very well. This is because functions are action-oriented and do not really correspond to the elements of the problem.
Some characteristics exhibited by procedure-oriented programming are:
· Emphasis is on doing things (algorithms).
· Large programs are divided into smaller programs known as functions. Most of the functions share global data.
· Data move openly around the system from function to function.
· Functions transform data from one form to another.
· Employs top-down approach in program design.
Object-Oriented programming
The major motivating factor in the invention of object-oriented approach is to remove some of the flaws encountered in the procedural approach. OOP treats data as a critical element in the program development and does not allow it to flow freely around the system. It ties data more closely to the functions that operate on it, and protects it from accidental modification from outside functions. OOP allows decomposition of a problem into a number of entities called objects and then builds data and functions around these objects. The data of an object can be accessed only by the functions associated with that object. However, functions of one object can access the functions of other objects.
Some of the striking features of object-oriented programming are:
· Emphasis is on data rather than procedure.
· Programs are divided into what are known as objects.
· Data structures are designed such that they characterize the objects.
· Functions that operate on the data of an object are tied together in the data structure.
· Data is hidden and cannot be accessed by external functions. .
· Objects may communicate with each other through functions.
· New data and functions can be easily added whenever necessary.
· Follows bottom-up approach in program design.
Object-oriented programming is the most recent concept among programming paradigms and still means different things to different people. It is therefore important to have a working definition of object-oriented programming before we proceed further. We define "object-oriented programming as an approach that provides a way of modularizing programs by creating partitioned memory area for both data and functions that can be used as templates for creating copies of such modules on demand." Thus, an object is considered to be a partitioned area of computer memory that stores data and set of operations that can access that data. Since
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
the memory partitions are independent, the objects can be used in a variety of different programs without modifications.
ii) Object oriented design
Design is concerned with the mapping of objects in the problem space into objects in the solution space, and creating an overall structure (architectural model) and computational models of the system. This stage normally uses the bottom-up approach to build the structure of the system and the top-down functional decomposition approach to design :the class member functions that provide services. It is particularly important to construct structured hierarchies, to identify abstract classes, and to simplify the inter-object communications. Reusability of classes from the previous designs, classification of the objects into subsystems and determination of appropriate protocols are some of the 'considerations of the design stage. The object oriented design (OOD) approach may involve the following steps:
1. Review of objects created in the analysis phase.
2. Specification of class dependencies.
3. Organization of class hierarchies.
4. Design of classes.
5. Design of member functions.
6. Design of driver program.
Review of Problem Space Objects
The main objective of this review exercise is to refine the objects in terms of their attributes and operations and to identify other objects that are solution specific. Some guidelines that might help the review process are:
1. If only one object is necessary for a service (or operation), then it operates only on that object.
2. If two or more objects are required for an operation to occur, then it is necessary to identify which object's private part should be known to the operation.
3. If an operation requires knowledge of more than one type of objects, then the operation is not functionally cohesive and should be rejected.
Class Dependencies
It is important to identify appropriate classes to represent the objects in the solution space and establish their relationships. The major relationships that are important in the context of design are:
1. Inheritance relationship
2. Containment relationship.
3. Use relationships
Organization of Class Hierarchies
This involves identification of common attributes and functions among a group of related classes and then combining them to form new class. The new class serves as the super class and the other as a subordinate classes.
Design of Class
Given below are some guidelines which should be considered while designing a class:
1. The public interface of a class should have only functions of the class.
2. An object of one class should not send a message directly to a member of another class.
3. A function should be declared public only when it is required to be used by the objects of the class.
4. Each function either accesses or modifies some data of the class it represents.
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
5. A class should be dependent on as few (other) classes as possible.
6. Interactions between two classes must be explicit.
7. Each subordinate class should be designed as a specialization of the base class with the
sole purpose of adding additional features.
8. The top class of a structure should represent the abstract model of the target concept.
Design of Member Functions
The member functions define the operations that are performed on the object's data.
These functions behave like any other C function and therefore we can use the top-down functional decomposition technique to design them.
Design of the Driver Program
The driver program is mainly responsible for:
1. Receiving data values from the user
2 Creating objects from the class definitions
3. Arranging communication between the objects as a sequence of messages for invoking the member functions, and
4. Displaying output results in the form required by the user.
(iii) User defined data types in C++
Structure and Classes
Data types such as struct and class is user defined data type in c++
example struct{ char title[50]; char author[50];} book;class student{char title[50]; char author[50];
public:
void get()}
student s1;
Q.41 When does ambiguity arise in multiple inheritance? How can one resolve it? Develop a program in C++ to create a derived class having following items: name, age, rollno, marks, empcode and designation. Design a base class student having data members as rollno, marks and the another base class is employee having data members as empcode and designation. These both base classes are inherited from a single base class person with data members name and age. The program should carry out the required input( ) and output( ) member functions for all. (12)
Ans:
A class can be derived such that it inherits the properties of two or more base classes. This is called multiple inheritance.
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
Ambiguity occurs when the base classes have functions with same name. for example if two base classes have the function get_data().then which function will be used by the derived class?
This problem can be solved be defining a named instance with the derived class, using the class resolution operator with the function name.
Program:
#include<conio.h>
#include<iostream.h>
class person{
public:
char name[20];
int age;
void get_details();
void disp_details();
};
void person::get_details() {
cout<<"enter name:";
cin>>name;
cout<<"enter age:";
cin>>age;
}
void person::disp_details() {
cout<<"name:"<<name;
cout<<'\n';
cout<<"age:"<<age;
cout<<'\n';
}
class student: public person {
public:
int roll_no;
int marks;
void get_details(int a,int b) {
roll_no = a;
marks = b;
}
void disp_data()
{
cout<<"roll_no:"<<roll_no;
cout<<'\n';
cout<<"marks:"<<marks; }
};
class employee:public person {
public:
int emp_code;
char desig[20];
void get_details(int a,char *d) {
emp_code = a;
strcpy(design, d); }
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
void disp_data() {
cout<<"emp_code:"<<emp_code;
cout<<'\n';
cout<<"designation:"<<desig; }
};
void main(){
int r,m,c;char d[20];
cout<<"\n Enter roll_no. and marks :";
cin>>r,m
cout<<"\n Enter employee code:";
cin>>c;
cout<<"\n Enter designation: ";
getline(d);
student ob1;
employee ob2;
ob1.get_details(r,m);
ob2.get_details(c,d);
ob1.disp_data();
ob2.disp_data();getch();
}
Q.42 Write two advantages of using include compiler directive. (4)
Ans: The include compiler directive is a preprocessor directive. Means, it is a command to the preprocessor. A preprocessor is a program that performs the preliminary processing with the purpose of expanding macro code templates to produce a C++ code. By including this header file, the function that we are using is replaced by the entire function definition. This is very helpful in case of small modules which are to be called several times in a program. It reduces the overhead cost of calling the function each time.
Q.43 Explain the difference between macro & function. (5)
Ans:Difference between macro and
function macro
Start of pre-processor directive
The preprocessor will replace all
the macro function used in the program by their function body before the
compilation. The distinguishing feature of macro function are that there will
be no explicit function call during execution, since the body is substituted at
the point of macro call during compilation. Thereby the runtime overhead for
the function linking or context-switch time is reduced. The directive # define
indicates the start of a macro function as shown in the figure.
![]() |
C++ statement![]()
![]()
![]()
#define MacroFuncName (parameters) Single line function body
Code: AC11 Subject: OBJECT ORIENTED PROGRAMMING
Examples:
#define inc(a) a+1
#define add(a, b) (a+b)
Function:A function is a set of program statements that can be processed independer.tly. A function can be invoked which behaves as though its code is inserted at the point of the function call. The communication between a caller (calling function) and callee (called function) takes place through parameters. The functions can be designed, developed, and implemented independently by different programmers. The independent functions can be grouped to form a software library. Functions are independent because variable names and labels defined within its body are local to it.
The use of functions offers flexibility in the design, development, and implementation of the program to solve complex problems. The advantages of functions includes the following:
· Modular programming
· Reduction in the amount of work and development time
· Program and function debugging is easier
· Division of work is simplitied due to the use of divide-and-conquer principle .
· Reduction in size of the program due to code reusability
· Functions can be accessed repeatedly without redevelopment, which in turn promotes reuse of code .
· Library of functions can be implemented by combining well designed, tested and proven function.
Every function has the following elements associated with it:
1. Function declaration or prototype.
2. Function parameters (formal parameters)
3. Combination of function declaration and its definition.
4. Function definition (function declaration and a function body).
5. return statement.
6. Function call.
A function can be executed using a function call in the program. The various components associated with the function are shown in Figure
Void func(int a, int b); Function declaration
Prototype
parameter
![]()
…………
void func(int a, int b) { Function definition
…… Body
}
func(x,y); Function Call