Oops Mock 1st
Oops Mock 1st
Section c:
Ans. #include<iostream>
Class student
{ Private:
Char name[10];
float average;
Public:
void compute();
void write_data();
};
void student::read_data()
cin>>name;
void student::compute()
if(s1<s2&&s1<s3)
else if(s2<s3)
average = (s1+s3)/2.0;
else
average = (s1+s2)/2.0;
void student::write_data()
cout<<"\n-------------------------------\n";
cout<<"\n-------------------------------\n";
}
int main()
student s[10];
int n,i;
cin>>n;
for(i=0;i<n;i++)
s[i].read_data();
for(i=0;i<n;i++)
s[i].compute();
for(i=0;i<n;i++)
s[i].write_data();
return 0;
Que2. What are constructors? Write a sample code to show the working of
constructors with inheritance.
Characteristics:
1) They should be declared as public.
2) They do not have any return type, not even void
3) By default they are treated as inline functions \, if defined inside the class
definition.
4) They can have default arguments, if required.
5) They cannot be inherited, through any derived class.
6) They cannot be declared as virtual.
Constructor definition:
The name of the constructor function should be same as the class name
// function body
Feet = f;
Inches =I;
#include<iostream>
Class parent
{
Public:
Parent ()
};
// sub class
Public:
Child ()
};
Int main()
Child obj;
Return 0;
Output:
Inside base class
Que3. What is the difference between overloading and overriding? Write a code
to show how overriding is achieved.
#include<iostream>
Public:
Void makesound()
{ cout<<”animal sound”;
};
Public:
Void makesound()
{ cout<<”Dogs bark”;
};
{ public:
Void makesound()
{ cout<<”cats meow”;
};
Int main ()
{
Animal a1;
A1.makesound ();
Cout<<endl;
Dog d1;
D1.makesound ();
Return 0;
Output:
Animals sound
Dogs bark
Cats meow
Ans.
#include<iostream>
Class NUM
Public:
{
N1 = x;
N2 = y;
N3 = z;
};
Void NUM::show ()
Void NUM::operator ++ ()
n1 =n1+5;
n2 = n2+10;
n3 = n3+15;
Void main()
Output:
N1 = 10
N2 = 20
N3 = 30
N1 = 15
N2 = 30
N3 = 45
#include<iostream>
Class weight
Private:
Int kg;
Public:
Weight ()
K=0;
Weight (int x)
Kg =x;
Void printweight()
Kg--;
};
Void main ()
Weight obj;
Obj.printweight ();
Obj--;
Cout<<”decrement weight”;
Obj.printweight ();
Output:
Que5. What do you mean by exception handling? Write a program to show how
it is achieved in c++.
Ans. The main objective of programming is to create error free programs. Errors
remain in the program due to mainly due to poor understanding of programming.
Error may be categorized into following two types.
a) Syntax error
b) Logical error
Errors which happen during compile time are known as syntax error or compile
time errors. On the other hand, errors which happen during execution of program
are known as run time error. Syntax error is not a problem as it can be easily
rectified. The main problem arises in case of run time error as it may cause minor
or major damage to the system.
Formally, an exception is defined as a run time error which may cause abnormal
termination of the program. C++ supports a mechanism known as exception
handling to control exceptions in a program.
A) Try
B) Throw
C) Catch
try
{
…………………………………………………..
…………………………………………………………
…………………………………………………………
……………………………………………….
……………………………………………..
…………………………………………….
#include<iostream>
Void main ()
Int n1,n2;
Cin>>n1>>n2;
Int x = n1 – n2;
If(x!=0)
Result = n1/x;
Cout<<”result is “<<result<<endl;
Else
Throw(x);
Catch (int y)
Output: 1
4 2
Result is 2
Output: 2
4 4
Exception caught as x becomes 0
Section B
Ans. Code in c
#include<stdio.h>
Int main ()
Return 0;
Output:
Hello world
Explanation:
Main (): the execution of a c program starts from the main () function.
Printf (): it is library function to send formatted output to the screen. In this
program the printf () function display hello world text on the screen.
Code in c++
#include<iostream>
Int main ()
Cout<<”hello world”;
Return 0;
Output:
Hello world
Explanation:
Cout<<”hello world”: the cout object belongs to the iostream file and the
purpose of the object is to display the content between quotes as it is on the
screen.
#include<iostream>
Class fact
Int n, I, facti;
Public:
n =x;
Facti = 1;
n = x.n;
facti = 1;
Void calculates ()
For (i=1;i<n;i++)
{
Void display ()
};
Int main()
Int x;
Cin>>x;
Fact f1(x);
F1.calculates ();
F2.dispaly ();
F2.calculates ();
F2.dispaly ();
Return 0;
}
Output:
Enter value: 5
Factorial: 120
Factorial: 120
Ans. Polymorphism is made of two Greek words “poly” and “morph”. Poly means
many and morph means several forms. So formally polymorphism can be defined
as below:
Definition: the process in which various forms of a single function can be defined
and utilized by different objects to perform similar types of operation. The
process of polymorphism is categorized into following hierarchy:
Polymorphism
Compile time
Run time
Polymorphism
Polymorphism
Virtual function
Function Operator Overloading
Overriding
Class Add
Public:
};
Int main ()
Add obj;
Return 0;
} output:
Output: 30
Output: 66
#include<iostream>
Class A
Public:
Void disp ()
};
Class B: public A
Void disp ()
};
Int main ()
{
A obj;
B. obj2;
Obj2. Disp();
Return 0;
Output:
Section A
Ans. Object: An object is a real world entity which has some features and
behaviors. An object has its own existence.
Class: A class is a collection of objects which posses similar features and behaviors
Ideally, class does not exist on its own. It exists due to the objects.
1. Class: student
Object: Rahul, mukesh, priya etc…
Features:
Name
Branch
Roll
Semester
Contact number
Behaviors:
Academic performance
Reading capacity
Understanding level
2. Class: bike
Object: pulsar, shine. Active etc…
Features:
Company name
Model name and number
Color
Behaviors:
Speed
Average
Mileage
Brake system
Sound system
Lamp
If we analyze the above two examples then we find that both class have some
objects. Objects of some student class possess similar properties whereas objects
of bike class possess similar properties. So, it is very important that the objects of
similar features must belong to the same class.
Ans. Inheritance:
Formally, inheritance can be defined as the process of creating a new class from
one or more existing classes. The existing class is known as base class or parent
class or super class whereas the newly created class is known as derived class or
child class or sub class. Inheritance provides a significant advantage in terms of
code reusability. Consider the diagram show below:
(Base class)
Class A
Inherit
Due to this the derived class B inherits the features of base class A. however;
the derived class can also have its own features. Now the terms reusability means
that the derived class object can access the base class members also.
Ans. Data encapsulation led to the important oop concept of data hiding.
The wrapping of data and functions into a single unit (called class) is known as
encapsulation.
By data encapsulation, data is not accessible to the outside the class. Only those
functions which are in the class can access.
Functions of the class provide the interface between the objects data and outside
objects/functions.
C++ supports the properties of encapsulation and data hiding through the
creation of user defined types, called classes.
#include<iostream>
Class myrank
Public:
};
Cin>>a;
Void myrank::print ()
}
Int main ()
Myrank k;
k.read ();
k.print ();
return 0;
Output:
Ans.
Ans. In c++ aggregation is a process in which one class defines another class as
any entity reference. It is another way to reuse the class. It is form of association
that represents HAS-A relationship.
Let’s see an example of aggregation where employee class has the reference of
address class as data members. In such way, it can reuse the members of Address
class.
#include<iostream>
Class Address
Public:
This->addressline = addressline;
This->city = city;
This->state = state;
}
};
Class employee
Private:
public:
int id;
string name;
This->id = id;
This->name = name;
This->address = address;
Void display()
Cout<<id<<””<<name<<””<<address->addressline<<””<<address-
>city<<””<<address->state<<endl;
};
Int main()
{
e1.display();
Output:
Ans. Abstraction refers to showing only essential features of the application and
hiding the details. In c++ classes provide methods to the outside world to access
and use the data variables, but the variables are hidden from direct access. This
can be done by specifiers.
For example: human being’s can talk, walk, hear, eat but the details are hidden
from the outside world. And second example of abstraction the real life example
of a man driving a car. The man only knows that pressing the accelerators will
increase the speed of car or applying brakes will stop the car but he does not
know about how on pressing accelerator the speed is actually increasing, he does
not know about the inner mechanism of the car or the implementation of
accelerator, brakes etc in the car.
#include<iostream>
Class implementAbstraction
{
Private:
Int a, b;
Public:
a = x;
b = y;
Void display ()
Cout<<”a = “<<a<<endl;
Cout<<”b = “<<b<<endl;
};
Int main ()
implementAbstraction obj;
obj.display ();
return 0;
}
Output:
a = 10
b = 20
Que2. Explain overriding with example.
Ans. If derived class defines same function as defined in its base class, it is known
as function overriding in c++. It is used to achieve runtime polymorphism. It
enables you to provide specific implementation of the function which is already
provided by its base class.
#include<iostream>
Class Animal
Public:
Void eat ()
Cout<<”eating “;
};
Public:
Void eat ()
Cout<<”eating bread”;
};
Int main ()
d.eat ();
return 0;
Section B
Que1. Write a sample code to show the structure of c++ program code.
C++ headers
Class definition
Member function definition
Main function
- Link section (c++ header)
Link section is where header files required for the program are included. Header
file consist function prototype and on program execution. They may predefined
like iostream, string, cmath etc. or user-defined.
In this section class used in the program are declared and/or defined. Body of
class is enclosed by curly brackets and ends with a semicolon. Class consists of
attributes and functions which are the members of that
Private:
Attributes;
Methods ();
Public:
Attributes;
Methods ();
Protected:
Attributes;
Methods ();
For example:
Class example
Private:
Int a, b;
Public:
};
Member function can be defined inside or outside class. If the member function is
defined outside the class, we need to use class name to which the function and
scope resolution (::) before function name.
Main function
Main is the most important function of c++ program. This is where the program
execution always begins. The main () function is compulsory in a c++ program.
Syntax:
Int main ()
Statement;
----------------------
Que2. Explain different types of inheritance with the help of a sample program.
Ans.