Vous êtes sur la page 1sur 22

Lab Manual

OBJECT ORIENTED CONCEPTS & C++

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


SCHOOL OF ENGINEERING & TECHNOLOGY
LIST OF PROGRAMS
1. Program in C++ to calculate the area of a circle, rectangle and triangle.

2. Program in C++ to print factorial of a given number.

3. Program in C++ to input marks in 5 subjects & calculate aggregate & percentage.

4. Program in C++ to find out whether the given number is odd or even

5. Program in C++ to find out whether the given year is a leap year or not

6. Program in C++ to find out the maximum out of three numbers

7. Program in C++ to generated Fibonacci series 0,1,1,2,3,5,8,13,...n

8. Program in C++ for swapping of two numbers with & without using 3rd variable.

9. Program in C++ to display series and find sum of 1+3+5+……..+n.

10. Program in C++ to implement single inheritance.


Program 1: Program in C++ to calculate the area of a circle, rectangle and triangle.
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float a,b,c,s,r,area;
int ch;
cout<<“***Menu***n1.Area of circlen2.Area of Rectangle”;
cout<<“/n3.Area of triangle/nEnter your choice:”;
cin>>ch;

switch(ch)
{
case 1:
{
cout<<“nEnter radius of the circle:”;
cin>>r;
area=3.14*r*r;
break;
}
case 2:
{
cout<<“nEnter length and breadth:”;
cin>>a>>b;
area=a*b;
break;
}
case 3:
{
cout<<“nEnter three sides of the triangle:”;
cin>>a>>b>>c;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
break;
}
default:
cout<<“nWrong choice…!!!”;
break;
}

cout<<“Area=”<<area;
getch(); //to stop the screen
}

Viva-Voce Questions
Q1: What is c++?
Ans: c++ is a general purpose object oriented programming language invented in the early 1980 by bajarne
stroutrup.

Q2. What is ‘cout’?


Ans. cout is the object of ostream class. The stream ‘cout’ is by default connected to console output device.

Q3. What is ‘cin’?


Ans. cin is the object of istream class. The stream ‘cin’ is by default connected to console input device.

Q4. What is a token?


Ans. C++ program consists of various tokens and a token is either a keyword, an identifier, a constant, a
string literal, or a symbol.

Q5. What is the full form of OOPS?


Ans. Object Oriented Programming System.
Program 2:Program in C++ to print factorial of a given number.
#include <iostream.h>
#include<conio.h>
void main()
{
clrscr()
int n, factorial = 1;
cout << "Enter a positive integer: ";
cin >> n;
for(int i = 1; i <=n; ++i)
{
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
return 0;
}

Viva-Voce Questions
Q1. What is the difference between C & C++?
Ans. C++ is an object oriented programing but c is a procedure oriented programing. C is super set of
C++. C can’t support inheritance, function overloading, method overloading etc. but C++ can do this. In c-
program the main function could not return a value but in the C++ the main function should return a value.

Q2. Different types of iteration statements available in C++ are


Ans. While, DoWhile, for

Q3. Different many types of operaters are available in C++


Ans. Arithmetic Operators,Relational Operators, Logical Operators, Bitwise Operators, Assignment
Operators, Misc Operators.

Q4.What are reserve words in C++.


Ans. Reserve words are the words that cannot be used as an identifier, such as the name of a variable,
function, or label – it is "reserved from use eg void, int float.

Q5. What is the character set of C++?


Ans The C++ supports a group of characters as listed below:-
1. Digits 0-9
2. Alphabets
i) Lower case letters a-z. ii) Upper case letters A-Z
3. Special characters +, -, *, /, !, @, #, $, %, &, ‘, <, >, ?, /, \, :, ; ...
Program 3: Program in C++ to input marks in 5 subjects and calculate aggregate and percentage.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int mark[5], i;
float sum=0;
cout<<"Enter marks obtained in Physics, Chemistry, Maths, CS, English :";
for(i=0; i<5; i++)
{
cin>>mark[i];
sum=sum+mark[i];
}
float avg=sum/5;
float perc;
perc=(sum/500)*100;
cout<<"Average Marks = "<<avg;
cout<<"\nPercentage = "<<perc<<"%";
getch();
}
Viva-Voce Questions
Q1. Define an array.
Ans. Array is collection of similar data items under a common name

Q2. What is the difference between the = symbol and == symbol?


Ans. The = symbol is often used in mathematical operations. It is used to assign a value to a given variable.
On the other hand, the == symbol, also known as “equal to” or “equivalent to”, is a relational operator that
is used to compare two values.

Q3. What are the differences between structures and arrays?


Ans: Structure is a collection of heterogeneous data type but array is a collection of homogeneous data types.

Q4. Why is it necessary to give the size of an array in an array declaration?


Ans: When an array is declared, the compiler allocates a base address and reserves enough space In memory
for all the elements of the array. The size is required to allocate the required space and hence size must be
mentioned.
Q5. What is the difference between Strings and Arrays?
Ans: String is a sequence of characters ending with NULL .it can be treated as a one dimensional Array of
characters terminated by a NULL character
Program 4: Program in C++ to find out whether the given number is odd or even
#include <iostream>
void main()
{
int n;
cout << "Enter an integer: ";
cin >> n;
if ( n % 2 == 0)
cout << n << " is even.";
else
cout << n << " is odd.";
}

Viva-Voce Questions
Q1.Differentiate between for loop and a while loop? What are it uses?
Ans: For executing a set of statements fixed number of times we use for loop while when the
number of iterations to be performed is not known in advance we use while loop.

Q2. In header files whether functions are declared or defined?


Ans: Functions are declared within header file. That is function prototypes exist in a header file,not function
bodies. They are defined in library (lib).

Q3. What is the data type to store the Boolean value?


Ans bool, is the new primitive data type introduced in C++ language

Q4..How variable declaration in c++ differs that in c?


Ans. C requires all the variables to be declared at the beginning of a scope but in c++ we can declare
variables anywhere in the scope. This makes the programmer easier to understand because the variables are
declared in the context of their use.

Q5.What is the difference between local variable and global variable?


Ans : Local variables are those variables which are declared within a function or a compound statement and
these variables can only be used within that function/scope. They cannot be accessed from outside the
function or a scope of it's declaration.
Global variables are those variables which are declared in the beginning of the program. They are not
declared within a function. So, these variables can be accessed by any function of the program. So, global
variables are global to all the functions of the program.
Program 5: Program in C++ to find out whether the given year is a leap year or not
#include <iostream>
void main()
{
int year;
cout << "Enter a year: ";
cin >> year;
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
cout << year << " is a leap year.";
else
cout << year << " is not a leap year.";
}
else
cout << year << " is a leap year.";
}
else
cout << year << " is not a leap year.";
}

Viva-Voce Questions
Q1.What is the scope resolution operator?
Ans. The scope resolution operator is used to
 Resolve the scope of global variables.
 To associate function definition to a class if the function is defined outside the class.

Q2. What are valid operations on pointers?


Ans. The only two permitted operations on pointers are
 Comparision ii) Addition/Substraction (excluding void pointers)

Q3. What is the default function call method?


Ans. By default the functions are called by value.

Q4. What is the difference between actual and formal parameters?


Ans. The parameters sent to the function at calling end are called as actual parameters while at the receiving
of the function definition called as formal parameters.

Q5. What are the major differences between Object Oriented Programming and Procedural Programming?
Ans. Object Oriented Programming
*Emphasis on data
*Follow bottom up approach in program design
*Concept of Data hiding prevents accidental change in the data
*Polymorphism, inheritance, Data Encapsulation possible
Procedural Programming
*Emphasis on doing things (function)
*Follow top-down approach in program design
*Due to presence of global variables, there are possibilities of accidental change in data
Program 6: Program in C++ to find out the maximum out of three numbers
#include <iostream>
void main()
{
float n1, n2, n3;
cout << "Enter three numbers: ";
cin >> n1 >> n2 >> n3;
if(n1 >= n2 && n1 >= n3)
{
cout << "Largest number: " << n1;
}
if(n2 >= n1 && n2 >= n3)
{
cout << "Largest number: " << n2;
}
if(n3 >= n1 && n3 >= n2)
{
cout << "Largest number: " << n3;
}
}

Viva-Voce Questions
Q1. What are constants?
 Constants are data items whose values cannot be changed.
 A constant is of numeric or non-numeric type.
 Numeric constants consist of only numbers, either whole numbers or decimal numbers.
 Integer, floating point are numeric constants.

Q2. What is built in data type?


Ans. Built in Data Type is also called as Fundamental or Basic data type. They are predefined in the
compiler.

Q3. What are the four storage specifiers in C++?


Ans There are four storage specifiers in C++:
 Auto
 Static
 Register and
 Extern

Q4.List out user defined data types?


Ans. User defined data types are
 Structure
 Union
 Class and
 Enumeration

Q5. What is abstraction?


Ans. Abstraction is of the process of hiding unwanted details from the user.
Program 7: Program in C++ to generated Fibonacci series 0,1,1,2,3,5,8,13,...n
#include <iostream>
void main()
{
int n, t1 = 0, t2 = 1, nextTerm = 0;
cout << "Enter the number of terms: ";
cin >> n;
cout << "Fibonacci Series: ";
for (int i = 1; i <= n; ++i)
{
// Prints the first two terms.
if(i == 1)
{
cout << " " << t1;
continue;
}
if(i == 2)
{
cout << t2 << " ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout << nextTerm << " ";
}
}

Viva-Voce Questions
Q1. What is Inheritance
Ans. Inheritance is the process of acquiring the properties of the existing class into the new class. The
existing class is called as base/parent class and the inherited class is called as derived/child class.

Q2.What is encapsulation?
Ans. The process of binding the data and the functions acting on the data together in an entity (class) called
as encapsulation.

Q3. What is a class?


Ans. Class is a blue print which reflects the entities attributes and actions. Technically defining a class is
designing an user defined data type.

Q4. What is an object?


Ans. An instance of the class is called as object.

Q5. What is the difference between the keywords struct and class in C++?
Ans. By default the members of struct are public and by default the members of the class are private.
Program 8: Program in C++ for swapping of two numbers with and without using 3rd variable.
#include <iostream>
void main()
{
int a = 5, b = 10, temp;
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
temp = a;
a = b;
b = temp;
cout << "\nAfter swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
}
WITHOUT USING THIRD VARIABLE
#include <iostream>
void main()
{
int a = 5, b = 10;
cout << "Before swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
a = a + b;
b = a - b;
a = a - b;
cout << "\nAfter swapping." << endl;
cout << "a = " << a << ", b = " << b << endl;
}
Viva-Voce Questions
Q1.Difference between pass by reference and pass by value?
Ans: Pass by reference passes a pointer to the value. This allows the callee to modify the variable
directly.Pass by value gives a copy of the value to the callee. This allows the callee to modify the value
without modifying the variable. (In other words, the callee simply cannot modify the variable, since it lacks
a reference to it.)

Q2. What is pointer?


Answer : Pointer is an address of a memory location. A variable, which holds an address of a memory
location, is known as a Pointer variable (or Simply Pointer). For example int *P;

Q3. What is difference between function overloading and operator overloading?


Ans. A function is overloaded when same name is given to different function.
While overloading a function, the return type of the functions need to be the same.

Q4. What is this pointer?


Ans. It is a pointer that points to the current object. This can be used to access the members of the current
object with the help of the arrow operator.

Q5. List out any four operators that can be overloaded?


 +
 ++
 –
 --
 *
Program 9: Program in C++ to display series and find sum of 1+3+5+……..+n.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,n,sum=0;
cout<<“1+2+3+……+n”;
cout<<“nEnter the value of n:”;
cin>>n;
for(i=1;i<=n;++i)
{
Cout<<i<<”\t”;
sum+=i;
}
cout<<“nSum=”<<sum;
getch();
}

Viva-Voce Questions
Q1. What is function overloading?
Defining several functions with the same name with unique list of parameters is called as function
overloading.

Q2.What is a constructor?
A constructor is the member function of the class which is having the same as the class name and gets
executed automatically as soon as the object for the respective class is created.

Q3. What does a destructor do?


Answer : A destructor deinitializes an object and deallocates all allocated resources.

Q4. Define Base class and derived class.


Answer : Base Class: A class from which another class inherits.
Derived Class: A class inheriting properties from another class.

Q5. What is a copy constructor?


A copy constructor is the constructor which take same class object reference as the parameter. It gets
automatically invoked as soon as the object is initialized with another object of the same class at the time of
its creation.
Program 10: Program in C++ to implement single inheritance.
#include<iostream>
#include<conio.h>
class staff
{
private:
char name[50];
int code;
public:
void getdata();
void display();
};

class typist: public staff


{
private:
int speed;
public:
void getdata();
void display();
};

void staff::getdata()
{
cout<<"Name:";
gets(name);
cout<<"Code:";
cin>>code;
}

void staff::display()
{
cout<<"Name:"<<name<<endl;
cout<<"Code:"<<code<<endl;
}

void typist::getdata()
{
cout<<"Speed:";
cin>>speed;
}

void typist::display()
{
cout<<"Speed:"<<speed<<endl;
}

int main()
{
typist t;
cout<<"Enter data"<<endl;
t.staff::getdata();
t.getdata();
cout<<endl<<"Display data"<<endl;
t.staff::display();
t.display();
getch();
return 0;
}

Viva-Voce Questions
Q1.List the types of inheritance supported in C++.
Single, Multilevel, Multiple, Hierarchical and Hybrid.

Q2. What is multiple inheritance?


A class can inherit properties from more than one class which is known as multiple inheritance
Write the characteristics of member functions?

Q3. Data members are the data variables that represent the features or properties of a class.
Member functions are the functions that perform specific tasks in a class.
Member functions are called as methods, and data members are also called as attributes.

Q4.What is mean by data hiding?


The members and functions declared under private are not accessible by members outside the class,
this is referred to data hiding.
Q5.What is public, private and protected?
Ans. Public, Protected and Private are three access specifier in C++.
Public data members and member functions are accessible outside the class.
Protected data members and member functions are only available to derived classes.
Private data members and member functions can't be accessed outside the class

Vous aimerez peut-être aussi