Nishant Oop
Nishant Oop
Write a C++ program to find out whether the given number is even or odd
(taking input from keyboard).
#include<iostream.h>
#include<conio.h>
void main()
{
int num;
clrscr();
cout<<"\nEnter a Number ";
cin>>num;
if(num%2==0)
{
cout<<"\nEntered number is even";
}
else
{
cout<<"\nEntered number is odd";
}
getch();
}
Constructor Destructor
Constructor helps to initialize the Whereas destructor is used to destroy
object of a class and allots the the instances.
memory to an object.
Constructors can take arguments Destructors do not take any
arguments.
Constructors can be overloaded. Destructors cannot be overloaded.
Write a program to overload the ‘—’ unary operator to negate the values. (Note: Any
other correct logic shall be considered) #include<iostream.h>
#include<conio.h>
#include<string.h>
class Number {
int x,y;
public:
Number (int a, int b) {
a =x;
b =y; }
void display() {
cout<<"value of x=”<<x<<”\n Value of y= ”<<y;
}
void operator - ( ) {
x = - x; y = - y;
} };
void main ()
{
Number N1(5,6);
clrscr ();
N1. display ();
-N1; cout<<"\n After negation:";
N1. display (); getch (); }
Write a program that copies contents of one file into another file. (Note: Any other
correct logic shall be considered) Assuming input file to be copied file1.txt contents are
“Hello Friends…” and file where the contents need to copy is file2.txt already created
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{ clrscr();
ifstream fs;
ofstream ft;
char ch, fname1[20], fname2[20];
cout<<"Enter source file name with extension (like files.txt) : ";
gets(fname1);
fs.open(fname1);
if(!fs)
{ cout<<"Error in opening source file..!!";
getch();
exit(1);
}
cout<<"Enter target file name with extension (like filet.txt) : ";
gets(fname2);
ft.open(fname2);
if(!ft)
{
cout<<"Error in opening target file..!!";
fs.close();
getch();
exit(2);
}
while(fs.eof()==0)
{
fs>>ch; ft<<ch; }
cout<<"File copied successfully..!!";
fs.close();
ft.close();
getch(); }
2 Data is less secured in C. In C++, you can use modifiers for class members to
. make it inaccessible for outside users.
3 C follows the top-down approach. C++ follows the bottom-up approach.
Program:
#include<iostream.h>
#include<conio.h>
class Subject1 { protected: float m1;
}; class Subject2 { protected: float m2; };
class Result:public Subject1,public Subject2
{
float Total;
public:
void accept()
{
cout<<"Enter marks of subject1:";
cin>>m1;
cout<<"\nEnter marks of subject2:";
cin>>m2;
} void calculate() {
Total=(m1+m2); }
void display() {
cout<<"\nSubject 1 marks:"<<m1;
cout<<"\nSubject 2 marks:"<<m2;
cout<<"\nTotal is:"<<Total;
}
};
void main()
{
Result r;
clrscr();
r.accept();
r.calculate();
r.display();
getch();
}
Write a C++ program to find the area of rectangle using class rectangle
which has following detailsi)
Accept length and breadth from user.
ii) Calculate the area
iii) Display the result.
Ans #include<iostream>
#include<conio.h>
using namespace std;
class rectangle
{
private:
int length;
int breadth;
public:
void accept()
{
cout<<"Enter length & breadth: \n";
cin>>length;
cin>>breadth;
}
void area()
{
int area;
area=length*breadth;
cout<<"\nArea of rectangle:"<<area;
}
};
int main()
{
rectangle r;
r.accept();
r.area();
getch();
}
Give syntax and use of fclose ( ) function. Syntax: int fclose(FILE* stream); Use: This function
is used to close a file stream. The data that is buffered but not written is flushed to the OS and all
unread buffered data is discarded.
Explain virtual base class with an example.
Virtual Base Class: An ancestor class is declared as virtual base class to avoid
duplication of inherited members inside child class due to multiple paths of
inheritance.
Consider a hybrid inheritance as shown in the above diagram. The child class has
two direct base classes, Parent1 and Parent2 which themselves have a common
base class as Grandparent. The child inherits the members of Grandparent via
two separate paths. All the public and protected members of Grandparent are
inherited into Child twice, first via Parent1 and again via Parent2. This leads to
duplicate sets of the inherited members of Grandparent inside Child class. The
duplication of inherited members can be avoided by making the common base
class as virtual base class while declaring the direct or intermediate base classes
as shown below.
Syntax:
class Grandparent
{
};
class Parent1:virtual public Grandparent
{
};
class Parent2: public virtual Grandparent
{
};
class Child: public Parent1,public Parent2 {
};
Example:
#include<iostream>
#include<conio.h>
using namespace std;
class student
{
int roll_no;
public:
void getnumber()
{
cout<<"Enter Roll No:";
cin>>roll_no;
}
void putnumber()
{
cout<<"\n\n\t Roll No:"<<roll_no<<"\n";
}
};
class test: virtual public student
{
public:
int test1,test2;
void getmarks()
{
cout<<"Enter Marks\n";
cout<<"Test 1:";
cin>>test1; cout<<"Test 2:";
cin>>test2;
}
void putmarks()
{
cout<<"\t Marks Obtained\n";
cout<<"\n\t Test 1 Marks :"<<test1;
cout<<"\n\t Test 1 Marks:"<<test2;
} };
class sports: public virtual student
{ public:
int score;
void getscore()
{
cout<<"Enter Sports Score:";
cin>>score;
}
void putscore() {
cout<<"\n\t Sports Score is:"<<score; } };
class result: public test, public sports
{
int total;
public:
void display()
{
total=test1+test2+score;
putnumber();
putmarks();
putscore();
cout<<"\n\t Total Score:"<<total;
}};
int main(){
result obj;
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
return 0; }
3-Differentiate between POP and POP
OOP. (any four points (4M) OOP
Object oriented. Structure oriented.
Program is divided into objects. Program is divided into functions.
Bottom-up approach. Top-down approach.
Inheritance property is used. Inheritance is not allowed.
It uses access specifier. It doesn’t use access specifier.
Encapsulation is used to hide the No data hiding.
data.
Concept of virtual function. No virtual function.
23 - Differentiate
between constructor
and destructor S. No. Constructor Destructor
Accept data for five students and display it. Write a C++
program to displya sum of array elements of array size n.
#include<iostream.h>
#include<conio.h>
void main() {
int arr[20],i,n,sum=0; clrscr();
cout<<"\nEnter size of an array:";
cin>>n;
cout<<"\nEnter the elements of an array:";
for(i=0;i<n;i++)
{ cin>>arr[i]; }
for(i=0;i<n;i++) {
sum=sum+arr[i]; }
cout<<"\nArray elements are:";
for(i=0;i<n;i++) { cout<<arr[i]<<" ";
} cout<<"\nSum of array elements is:"<<sum;
getch(); }
Describe following terms: Inheritance, data abstraction, data
encapsulation, dynamic binding.
Inheritance:
1. Inheritance is the process by which objects of one class acquire
the properties of objects of another class.
2. It supports the concept of hierarchical classification. It also
provides the idea of reusability.
Data abstraction:
1. Data abstraction refers to the act of representing essential features
without including the background details or explanations.
2. Classes use the concept of abstraction and are defined as a list of
abstract attributes such as size, weight and cost and functions to
operate on these attributes.
Data encapsulation:
1. The wrapping up of data and functions together into a single unit
(called class) is known as encapsulation.
2. By this attribute the data is not accessible to the outside world,
and only those functions which are wrapped in the class can
access it.
Dynamic Binding:
1. Dynamic binding refers to the linking of a procedure call to be
executed in response to the call.
2. It is also known as late binding. It means that the code associated
with a given procedure call is not known until the time of the call
at run-time.
Write a C++ program to append data from abc.txt to xyz.txt file.
(Note: Any other correct logic shall be considered)
Assuming input file as abc.txt with contents "World" and output file
named as xyz.txt with contents "Hello" have been already created.
#include <iostream.h>
#include<fstream.h>
int main()
{
fstream f;
ifstream fin;
fin.open("abc.txt",ios::in);
ofstream fout;
fout.open("xyz.txt", ios::app);
if (!fin) {
cout<< "file not found";
} else {
fout<<fin.rdbuf();
} char ch;
f.seekg(0);
while (f) {
f.get(ch);
cout<< ch;
}
f.close();
return 0; }
Write any four benefits of OOP. Benefits of OOP:
1. We can eliminate redundant code and extend the use of existing classes.
2. We can build programs from the standard working modules that communicate with one
another, rather than having to start writing the code from scratch. This leads to saving of
development time and higher productivity.
3. The principle of data hiding helps the programmer to build secure programs that cannot be
invaded by code in other parts of the program.
4. It is possible to have multiple instances of an object to co-exist without any interference.
# include <iostream.h>
#include<conio.h>
class College_Student
{
int student_id;
char College_code[5];
public:
void read_collegeStud_Data()
{
cout<<”Enter college code and student id\n”;
cin>>college_code>>student_id;
}
void display_collegeStud_Data()
{
cout<<”\ncollege code\tstudent id\n”;
cout<<college_code<<”\t”<<student_id<<”\n”;
}
};
class test: virtual public College_Student
{
float percentage;
public:
void read_test()
{
cout<<”\n Enter test percentage\n”;
cin>> percentage;
}
void display_test()
{
cout<<”\n test percentage:”<<percentage;
}
};
class sports: virtual public College_Student
{
char grade[5];
public:
void read_sportsData(){
cout<<”\n Enter sport grade\n”;
cin>> grade;
}
void display_sportsData()
{
Cout<<”\n sport grade:”<<grade;
}
};
class result: public test, public sports
{
public:
void read_result()
{
read_collegeStud_Data() ;
read_test()
read_sportsData();
}
void display_result()
{
display_collegeStud_Data() ;
display_test()
display_sportsData();
}
};
void main()
{
result r;
clrscr();
r.read_result();
r.display_result();
}