Answer Bank For OOP CT2
Answer Bank For OOP CT2
BEFORE THIS DOCUMENT NOTE THAT U ARE USING THIS AT UR OWN RISK AND I
AM NOT RESPONISBLE IF U GET LESS MARKS IN OOP
Questions:
w.r.t file
Ans:
File
file
12. Give syntax to define derived class constructor and describe the order of
execution of constructor for following Figure.
Ans:
Syntax:
class derived-class_name : visibiliy_mode base_class_name
In the following figure class C will be executed first then Class B and then Class A
13. Describe the use of Virtual Function.
Ans:
Ans:
#include<iostream.h>
#include<conio.h>
class college
{
protected:
int college_code;
public:
void get_code(int a)
{
college_code=a;
}
void put_code()
{
cout<<”College Code: “college_code;
}
};
class test : public college
{
protected:
int percentage;
public:
void get_perc(int b)
{
percentage=b;
}
void put_data()
{
cout<<”Percentage: “<<percentage;
}
};
class sports : public college
{
protected:
char grade;
public:
void get_grade(char c)
{
grade=c;
}
void put_grade()
{
cout<<”Grade: “<<grade;
}
};
class result: public test, public sports
{
public:
void display()
{
put_code();
put_data();
put_grade();
}
};
void main()
{
result r1;
r1.get_code(0568);
r1.get_perc(90);
r1.get_grade(A);
r1.display();
getch();
}
void main()
{
model m1;
m1.get_type(Electric);
m1.get_data1(Tesla,120);
m1.get_data2(Model Y,5000000);
m1.display();
getch();
}
23. Write a program in C++ to overload unary ‘+’ operator to concatenate two
strings.
Ans:
24. Write a C++ program to declare a class product with members as
product_no and name. Accept and display data for one object of product.
Use pointer to object to call functions of class.
Ans:
#include<iostream.h>
#include<conio.h>
class product
{
private:
int product_no;
char name;
public:
void getdata()
{
cout<<”Enter Product No and Name: “;
cin>>product_no>>name;
}
};
void product :: show_data()
{
cout<<”Product No: “<<product_no;
cout<<”Name: “<<name;
}
int main()
{
product p, *ptr;
ptr=&p;
p->show_data();
return 0;
}
25. Write a C++ program to declare a class employee with members as
employee_no and name. Accept the information for 5 objects and
display it using pointer to array of objects.
Ans:
#include<iostream.h>
#include<conio.h>
class employee
{
int emp_no;
char name;
public:
void setdata(int a,char b)
{
emp_no=a;
name=b;
}
};
void employee :: showdata()
{
cout>>”Employee NO: ”>>emp_no;
cout>>”Name: “>>name;
}
int main()
{
int p,i;
char q;
employee e[5],*ptr;
for(i=0;i<5;i++)
{
cout<<”Enter Emp Number and Name”<<i+1<<endl;
cin>>p>>q;
ptr->setdata(p,q);
ptr++;
}
for(i=0;i<5;i++)
{
cout<<”Employee Details: “<<i+1<<endl;
ptr->showdata();
ptr++;
}
return 0;
}
26. Write a C++ program to write ‘Welcome to First lecture’ in a file. Then
read the data from file and display it on screen.
Ans:
27. Write a C++ program to copy content of one file to another file.
Ans:
28. Write a C++ program to calculate number of words in the “abc.txt” file.
Ans:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin("fname.txt"); //opening text file
int word=1; //will not count first word so initial value is 1
char ch;
fin.seekg(0,ios::beg); //bring position of file pointer to begining of file
while(fin)
{
fin.get(ch);
if(ch==' '||ch=='\n')
word++;
}
cout<<"\nWords="<<word<<"\n";
fin.close(); //closing file
return 0;
}
29. Write a program to define add() function to add two integers and to add
three integers.
Ans:
30. Write a program to overload ‘+‘ operator to add two time objects
having data members as hours and minutes. Display the result using
third object.
Ans:
#include<iostream.h>
#include<conio.h>
class time
{
int hours,mins;
int result;
public:
void getdata()
{
cout<<”Enter Hours And Mins: “;
cin>>hours>>mins;
}
void operator +()
{
hours++;
mins++;
}
void display()
{
result=hours;
cout<<” Hours: “<<hours;
result=mins;
cout<<”Mins :”<<mins;
}
};
void main()
{
time t;
t.getdata();
+t;
t.display();
}