Oops Programs
Oops Programs
Source Code:
#include<iostream>
using namespace std;
int main ()
{
cout<<”Hello world”;
return 0;
}
Source Code:
#include<iostream>
using namespace std;
int main()
{
int n,i,f=1;
cout<<"Enter no";
cin>>n;
if(n==0)
{
cout<<"Factorial"<<f;
}
else
for(i=1;i<=n;i++)
{
f=f*i;
}
cout<<"Factorial"<<f;
return 0;
}
VIVA QUESTIONS 1
Ans. The main function is a special function. Every C++ program must contain a func-
tion named main. It serves as the entry point for the program. The computer will start
running the code from the beginning of the main function.
Q.3 What is the diffence between function declaration and function definition?
Ans. A function declaration tells the compiler about a function's name, return type, and
parameters. A function definition provides the actual body of the function.
Q.4 If the function doesn’t return a value, how to declare that function?
Ans. A function that doesn't return a value is declared with return type “void”.
Q.5 What are cout and cin? Why are they used? Which library is used to include them?
Ans. cin is an object of the input stream and is used to take input from input streams like
files, console, etc. cout is an object of the output stream that is used to show output. Basi-
cally, cin is an input statement while cout is an output statement. To use cin and cout in
C++ one must include the header file iostream in the program.
Program No: 2
Aim: WAP to initialize and accessing values of data members using structure.
Source Code:
#include <iostream>
using namespace std;
struct Point {
int x = 0; // It is Considered as Default Arguments and no Error is Raised
int y = 1;
};
int main()
{
struct Point p1;
VIVA QUESTIONS 2
Here, a structure variable bill is defined which is of type structure Person. When
structure variable is defined, only then the required memory is allocated by the com-
piler.
bill.age = 50;
Aim: WAP to find area and volume of a room using class and objects.
Source Code:
// create a class
class Room {
public:
double length;
double breadth;
double height;
double calculateArea() {
return length * breadth;
}
double calculateVolume() {
return length * breadth * height;
}
};
int main() {
Aim: WAP to calculate area of square and rectangle using function overloading.
#include<iostream>
using namespace std;
void area(int);
void area(int,int);
int main()
{
area(5);
area(4,8);
return 0;
}
void area(int s)
{
int a;
a=s*s;
cout<<"the area of square is="<<a<<endl;
}
void area(int l,int b)
{
int a;
a=l*b;
cout<<"the area of rectangle is ="<<a<<endl;
}
#include<iostream>
using namespace std;
void area(int);
void area(int,int);
int main()
{
area(5);
area(4,8);
return 0;
}
void area(int s)
{
int a;
a=s*s;
cout<<"the area of square is="<<a<<endl;
}
void area(int l,int b)
{
int a;
a=l*b;
cout<<"the area of rectangle is ="<<a<<endl;
}
VIVA QUESTIONS 3
Program No: 4
Aim: WAP to illustrate dynamic allocation and deallocation of memory using new and
delete.
Source Code:
#include <iostream>
using namespace std;
int main ()
{
// Pointer initialization to null
int* p = NULL;
if (!q)
cout << "allocation of memory failed\n";
else
{
for (int i = 0; i < n; i++)
q[i] = i+1;
Aim: WAP to store more than one Employee’s details with an Employee id and name
implementing array of objects
Source Code:
#include<iostream>
using namespace std;
class Employee
{
int id;
char name[30];
public:
// Declaration of function
void getdata();
// Declaration of function
void putdata();
};
// Driver code
int main()
{
// This is an array of objects having
// maximum limit of 30 Employees
Employee emp[30];
int n, i;
cout << "Enter Number of Employees - ";
cin >> n;
VIVA QUESTIONS 4
Malloc() Calloc()
It is a function that creates one It is a function that assigns more than one
block of memory of a fixed size. block of memory to a single variable.
It only takes one argumemt It takes two arguments.
It is faster then calloc. It is slower than malloc()
It has high time efficiency It has low time efficiency
It is used to indicate memory alloca- It is used to indicate contiguous memory
tion allcoation