The document is a question bank for a course on Object Oriented Programming in C++. It covers various topics such as polymorphism, file operations, virtual functions, inheritance, and function overloading, providing definitions, syntax, examples, and programming tasks. The document includes questions that require explanations, code implementations, and comparisons of concepts in C++.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
7 views9 pages
OOP QB(CT-2)
The document is a question bank for a course on Object Oriented Programming in C++. It covers various topics such as polymorphism, file operations, virtual functions, inheritance, and function overloading, providing definitions, syntax, examples, and programming tasks. The document includes questions that require explanations, code implementations, and comparisons of concepts in C++.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9
Question Bank for CT-2 There are two types of polymorphism:
Course & Code: OBJECT ORIENTED 1.Compile time polymorphism :- This
PROGRAMMING IN C++ information is available at the compile time 2 Marks: and, therefore, compiler selects the Q.1 Give syntax and use of fclose ( ) appropriate function at the compile time. function. 2.Run time polymorphism :- Run time --> Syntax: polymorphism is achieved when the int fclose(FILE* stream); object's method is invoked at the run Use: This function is used to close a file time instead of compile time. stream. The data that is buffered but not written is flushed to the OS Q.5 What are the rules for virtual function and all unread buffered (write any four) data is discarded. --> Rules for virtual function: 1. The virtual functions must be members of Q. 2 Explain reference and dereference some class. operates w.r.t pointer. 2. They cannot be static members. --> 1. Reference Operator: (&) The reference 3. They are accessed by using object operator is used to obtain the address of a pointers. variable. When you use (&) before variable, 4. A virtual function can be a friend of it returns the memory adchess of that another class. variable. This address be stored in a pointer. 5. The prototypes of the base class version 2. Dereference Operator: (*) The of a virtual function and dereference operator is used to access the all the derived class versions must be value at the address stored in pointer. When identical. you use * before a pointer, it retrieves the 6. We cannot have virtual constructors, but value stored at memory address the pointer we can have virtual destructors. is pointing too. Q. 6 State the need of virtual function in C+ Q.3 Explain ios :: app and ios :: in flags + --> ios : : in : It is a file mode. It is used to --> There is a necessity to use the single open a file in read only mode. pointer to refer to all the objects of the ios:: app = open a file for output and move different classes. So, we create the pointer to end of existing data (normally used for to the base class that refers to all the append data to a file, but data can be derived objects. But, when base class written anywhere in file). pointer contains the address of the derived class object, always executes the base class Q.4 Define polymorphism with it’s types. function. This issue can only be resolved by --> Polymorphism means "many forms", using the 'virtual' function. and it occurs when we have many classes that are related to each other by inheritance. 7.Define file with its operations. -->A file is a collection of related data stored int roll_no; in a particular area on the disk. }; File Operations: class Info:public Teacher,public Student Open file { Close file public: Read file void acceptT() Write file { Update file cout<<"\nEnter data for teacher:"; cout<<"\nName:"; 8.List c++ stream classes along with their cin>>Name; function. (any two classes) cout<<"\nEmployee id:"; -->1. ifstream: This class is used for reading cin>>emp.id; data from files. It provides functions like } open(), close(), and getline() to read data void displayT() from a file into a program. { 2. ofstream: This class is used for writing cout<<"\nTeacher's data is:"; data to files. It provides functions like cout<<"\nName:"<<Name; open(), close(), and write() to output data cout<<"\nEmployee id:"<<emp.id; from a program to a file. } void acceptS() 4 Marks { cout<<"\nEnter student's data:"; Q.1 Write a C++ program to implement cout<<"\nName:"; inheritance shown in following figure: cin>>s.name; cout<<"\nRoll no:"; cin>>roll_no; } void displayS() { -->#include<iostream.h> cout<<"\nStudent's data is:"; #include<conio.h> cout<<"\nName:"<<s.name; class Teacher cout<<"\nRoll no:"<<roll_no; { } protected: }; char Name[20]; void main() int emp.id; { }; Info I; class Student clrscr(); { I.acceptT(); protected: I.displayT(); char s.name[20]; I.acceptS(); I.displayS(); In the above example, this pointer is used to getch(); represent object s when } setdata ( ) and putdata ( ) functions are called. Q.2 Describe 'this' pointer with an example -->‘this’ pointer: Q.3 Write a program to count the number C++ uses a unique keyword called „this‟ to of lines in file. represent an object that --> #include<iostream.h> invokes a member function. This unique #include<fstream.h> pointer is automatically #include<conio.h> passed to a member function when it is void main() invoked. "this" is a pointer { that always point to the object for which ifstream file; the member function was char ch; called. int n=0; For example, the function call A.max ( ) will clrscr(); set the pointer „this‟ to file.open("abc.txt"); the address of the object A. Then suppose while(file) we call B.max ( ), the { pointer „this‟ will store address of object B. file.get(ch); Example: if(ch=='\n') #include<iostream.h> n++; class sample } { cout<<"\n Number of lines in a file are:"<<n; int a; file.close(); public: getch(); void setdata(int x) } { Q. 4 Differentiate between run time and this ->a=x; compile time polymorphism. } -- > void putdata() Compile time Runtime { polymorphism polymorphism cout<<this ->a; In this In this } polymorphism, an polymorphism, }; object is bound to selection of void main() its function call at appropriate { compile time. function is done at run time. sample s; Functions to be Function to be s.setdata(100); called are known called is unknown s.putdata( ); well before. until appropriate } selection is made. cout<<" The Marks of two subject This does not This requires use of are:"<<marks1<<marks2<< endl; require use of pointers to object } pointers to objects class Sports Function calls Function calls { execution are faster execution are protected: slower int sportmarks, It is implemented It is implemented public: with operator with virtual void get overloading or function. { function overloading cout<<" Enter Sports marks:"; cin>> sportsmarks; } void put() Q. 5. Write a program to implement { multiple inheritance as shown in following cout<<" The Marks of Sport is: Figure No. 1: "<<Sportsmarks << endl; } }; Class Result: public Test, public sports { int total; public: void cal() { total = marks1+marks2+ sportsmarks; #include <iostream.h> cout<<" The total is: "<<total <<endl; #include <conio.h> } class Test }; { void main() { protected: Result r; int marks1, marks2; r. accept(); public: r. display(); void accept() r. get(); { r. put(); cout<<" Enter Marks of two subject."; r. cal(); cin>> marks1>>marks2; getch(); } } void display() { Q6. Write a C++ program to copy the contents of a source file student 1.txt to a destination file student 2.txt using file Q. 7. Explain virtual base class with an operations. example. #include <iostream.h> Virtual Base Class: #include <conio.h> An ancestor class is declared as virtual base #include <fstream.h> class which is used to #include <stdlib.h> avoid duplication of inherited members void main() inside child class due to { multiple path of inheritance. Clrscr(); ifstream fs; ofstream ft; char ch, student1 [20], student2[20], cout<<"Enter source filename with extension (like files.txt): "; gets (student1); fs. open (student 1); Consider a hybrid inheritance as shown in if (!fs) { the above diagram. The cout<<" Error in opening source file..!!"; child class has two direct base classes, getch(); Parent1 and Parent2 which exit(1); themselves have a common base class as } Grandparent. The child cout<<"Enter target file name with inherits the members of Grandparent via exterision (like file1.txt): two separate paths. All the gets (Student2); public and protected members of ft. open (student2); Grandparent are inherited into Child if (!ft) { twice, first via Parent1 and again via cout<<" Error in opening target file..!!"; Parent2. This leads to duplicate fs.close(); sets of the inherited members of getch(); Grandparent inside Child class. The exit(2); duplication of inherited members can be } avoided by making the while (fs. eof() == 0) { common base class as virtual base class fs>>ch; while declaring the direct or ft<<ch; intermediate base classes as shown below. } class Grandparent cout<<" File copied successfully..!!"; { fs.close(); }; ft. close(); class Parent1:virtual public Grandparent getch(); { } }; class Parent2:virtual public Grandparent { { }; public: class Child: public Parent1,public Parent2 int score; { void getscore() }; { Example cout<<"Enter Sports Score:"; #include<iostream.h> cin>>score; #include<conio.h> } class student void putscore() { { int rno; cout<<"\n\t Sports Score is:"<<score; public: } void getnumber() }; { class result: public test, public sports cout<<"Enter Roll No:"; { cin>>rno; int total; } public: void putnumber() void display() { { cout<<"\n\n\t Roll No:"<<rno<<"\n"; total=part1+part2+score; } putnumber(); }; putmarks(); class test: virtual public student putscore(); { cout<<"\n\t Total Score:"<<total; public: } int part1,part2; }; void getmarks() void main() { { cout<<"Enter Marks\n"; result obj; cout<<"Part1:"; clrscr(); cin>>part1; cout<<"Part2:"; obj.getnumber(); cin>>part2; obj.getmarks(); } obj.getscore(); void putmarks() obj.display(); { getch(); cout<<"\t Marks Obtained\n"; } cout<<"\n\t Part1:"<<part1; cout<<"\n\tPart2:"<<part2; Q.8 Write a C++ program to overload “+” } operator so that it will perform }; concatenation of twostrings. (Use class get class sports: public virtual student data function to accept two strings) ---> #include<iostream.h> { #include<conio.h> int temp; #include<string.h> temp=a; class opov a=b; b=temp; { cout<<"\nInteger values after swapping char str1[10]; are:"<<a<<" "<<b; public: } void getdata() void swap(float x,float y) { { cout<<"\nEnter a strings"; float temp1 = x; cin>>str1; x=y; } y=temp1; void operator +(opov o) cout<<"\nFloat values after swapping { are:"<<x<<" "<<y; cout<<strcat(str1,o.str1); } } void main() }; { void main() clrscr(); { swap(10,20); opov o1,o2; swap(10.15f,20.25f); clrscr(); getch(); o1.getdata(); } o2.getdata(); o1+o2; Q.10 Write a C++ program to write getch(); ‘Welcome to poly’ in a file. Then read the } data from file and display it on screen. Q.9 Describe Function overloading with #include<iostream.h> suitable program. #include<conio.h> ---> Function overloading refers to the use #include<fstream.h> of the same function name to create void main() functions that perform a variety of different { tasks. Each function has same name but char str[25] = "Welcome to poly",ch; different number of arguments or different clrscr(); types of arguments.The function is executed ofstream fout; depending on the argument list in the fout.open("output.txt"); function call. fout<<str; Program: fout.close(); #include<iostream.h> ifstream fin; #include<conio.h> fin.open("output.txt"); void swap(int a,int b) while (!fin.eof()) { ptr = new Item; fin.getline(str, 25); ptr->accept( ); cout<<str<<endl; ptr->display( ); } delete ptr; fin.close(); getch( ); getch(); } } Q. 12 Explain virtual function with example. Q. 11 Write a program to declare a class 1. In C++, a virtual function is a member ‘item’ containing data members as function in a base class that you expect to ‘item_name’, ‘code’, ‘price’. Accept and be redefined in derived classes. When a display the information for one object using function is marked as virtual in the base pointer to that object. class, it enables runtime polymorphism, #include <iostream> meaning the decision of which function to #include <conio.h> call (base or derived class version) is made class Item { during program execution. char item_name [30]; 2. Normally, C++ uses compile-time binding, int code; where the compiler determines which float price; function to call based on the pointer type. However, with virtual functions, dynamic (or public: late) binding occurs, meaning the function void accept() { call is resolved at runtime. This allows the cout << "Enter item name: "; base class pointer to call the derived class's cin >> item_name; function, enabling more flexible and cout << "Enter item code: "; reusable code. cin >> code; #include <iostream> cout << "Enter item price: "; class Base { cin >> price; public: } virtual void show() { cout << "Base class function called" << void display(){ endl; cout << "\nItem Name: " << } item_name; }; cout << "\nItem Code: " << code; class Derived : public Base { cout << "\nItem Price:" << price << public: endl; void show() override { } cout << "Derived class function called" }; << endl; } void main( ) { }; Item *ptr; int main() { { Base *bptr; cout<<" Enter subject name"; Derived d; cin>> subject; bptr = &d; } bptr->show(); void put() { return 0; cout<<" The subject taught by teacher is." } <<subject << endl; In the above code, show() in the base class } is declared as a virtual function. }; class officer: public Staff Q. 13 Write a program to implement { inheritance as shown in figure No. 2. public: Assume suitable member function. char grade [2]; void getdata() { cout << "Enter Grade:"; cin >> Grade; } #include <iostream.h> void putdata() #include <conio.h> { class staff cout<<"Grade of the officer is: " <<grade; { } public: }; int code; void main() void accept() { { Teacher t; cout << "Enter the staff code:"; Object o; cin> code; t. accept (); } t.display(); void display() t.get(), { t. put(); cout<<"The staff code is: " << code <<endl; o.accept(); } o. podisplay(); }; o. getdata(); class Teacher: public staff o. putdata(); { getch(); public: } char subject [20]; void get()