1. The document contains questions about classes and objects in C++ including constructors, destructors, overloaded constructors, and copying objects.
2. It asks the student to define several classes with private data members and public member functions like constructors, destructors, and other methods.
3. It provides code snippets and asks the student to predict the output, identify features of constructors and destructors, and when they are called.
1. The document contains questions about classes and objects in C++ including constructors, destructors, overloaded constructors, and copying objects.
2. It asks the student to define several classes with private data members and public member functions like constructors, destructors, and other methods.
3. It provides code snippets and asks the student to predict the output, identify features of constructors and destructors, and when they are called.
WORKSHEET – 3 -TOPIC : CLASSES & OBJECTS & CONSTRUCTORS & DESTRUCTORS 1. What are the characteristics of a constructor? 2. Explain 3 types of constructor with example in C++. 3 What is the difference between a default constructor and constructor with default arguments? 4. Illustrate overloaded constructor. 5. Mention any two similarities and differences between a constructor and a destructor. 6. Write the function header for constructor and destructor of class Flight. 7 Find the output of the following program snippet ( Assume the necessary header files are included) class Olympiad { int SchoolCode; char name[20]; int participants; public : Olympiad( ) { SchoolCode = 1000; strcpy(name, ”unknown“) , participants=1;} Olympiad( int sc, char N[ ], int num=1) { SchoolCode = sc; strcpy(name,N); participants = num;} int add( int x) { participants += x return participants;} ~Olympiad( ) { cout<<”processed data for ”<<name;} }; void main( ) { Olympiad Science(2001, “BRM”, 344), Cyber; cout<<endl<< Cyber.add(50) <<endl<< Science.add(2)<<endl;} 8 Find the output of the following C++ program: [3] #include<iostream.h> #include<conio.h> #include<ctype.h> class Class { int Cno,total; char section; public: Class(int no=1) { Cno=no; section='A'; total=30; } void addmission(int c=20) { section++; total+=c; } void ClassShow() { cout<<Cno<<":"<<section<<":"<<total<<endl;} } ; void main() { Class C1(5),C2; C1.addmission(25); C1.ClassShow(); C2.addmission(); C1.addmission(30); 1 C2.ClassShow(); C1.ClassShow(); } 9. Define a class Tour with the following specifications Private members Tcode string NoofAdults integer Noofkids integer Kilometers integer TotalFare float Public members A constructor to assign initial values as follows Tcode with the word NULL and others with zero A function Assignfare() which calculates and assigns the value of the data member Totalfare as follows For each Adult Fare(Rs.) For Kilometres 500 >=1000 300 <1000 & >=500 200 <500 For each kid the above fare will be 50% of the fare mentioned in the above table For example : If Kilometres is 850, NoofAdults = 2 and Noofkids = 3 Then Totalfare should be calculated as NoofAdults*300 + Noofkids*150 i.e. 2*300 + 3*150 = 1050 A function EnterTour() to input the values of the data members Tcode, NoofAdults, Noofkids and Kilometres and invoke the Assignfare() function A function ShowTour() which displays the content of all the data members for a Tour. 10. Answer the questions (i) and (ii) after going through the following class class Maths { char chapter[20]; int marks; public: Maths() // member function 1 { strcpy(chapter, “Geometry”); marks=10; cout<< “Chapter initialized”; } ~Maths() // member function 2 { cout<< “Chapter over !”; } }; (i) Name the specific features of class shown by Member function 1 and Member function 2 in the above example. (ii) When would Member function 1 and Member function 2 get executed ? 11. Answer the questions (i) to (iv) after going through the following class. class player { int health, age; public : player() { health = 7; age = 17; } //Function 1 player( int h, int a) { health=h; age= a; } // Function 2 player(player &p) { ……} // Function 3 ~player() { cout << “\nMemory free”; } }; 2 void main() { player p1; //statement 1 player p2 = p1; //statement 2 } Answer these questions: (i) Write the name of Function 1, Function 2 and Function 3. (ii) When p2 object is created, specify which function gets invoked and why? iii) Write the complete definition of Function 3 iv) Write the statement to invoke Function 2 Answer the questions (i) and (ii) after going through the following C++ class: class Product { char Name[10] ; int Stock ; float Price ; public: Product( ) ------- function1 {strcpy( Name, “Oil”) ; Stock = 75; Price = 110; } Product( char * m , int n , float p ); --------function2 Product( Product &) ; --------function3 ~Product( ); --------function4 void check( Product X ) --------function5 { ( Stock < X.Stock) ? cout << Name << “:” << 0.05 * Price : cout << X.Name << “:” << 0.1 * X.Price ; } }; void main ( ) { Product P1( “Soap”, 155, 32) ; Product P2 = P1; Product( ) . check(P2) ; } 1)Name function3 in the above code; Which statement(s) in the main() would have invoked function3. II)If the above functions are complete and the code is executed, what will be the output? 12. Define a class Play with the following specifications : Private members Playcode integer Playtitle 25 characters Duration float Noofscenes integer Public member functions A constructor function to initialize Duration as 45 and Noofscenes as 5. Newplay() function to accept values for Playcode and Playtitle. Moreinfo() function to assign the values of Duration and Noofscenes with the help of corresponding values passed as parameters to this function. Showplay() function to display all the data members on the screen. 13. Predict the output: #include<iostream.h> class A { public: A() { show(); cout<<"\t"<<"constructor\n"; } void calculate() { cout<<"\t"<<"calculating\n"; } void show() 3 { calculate(); cout<<"I am displaying\n"; } }; void main() { A one; } 14. Define a class Customer with the following specifications. Private Members : Customer_no integer Customer_name char (20) Qty integer Price, TotalPrice, Discount, Netprice float Member Functions: Public members: * A constructor to assign initial values of Customer_no as 111,Customer_name as “NULL”, Quantity , Price, Discount and Netprice as 0. *Input( ) – to read data members(Customer_no, Customer_name, Quantity and Price) call Caldiscount(): * Caldiscount ( ) – To calculate Discount according to TotalPrice and NetPrice TotalPrice = Price*Qty TotalPrice >=50000 – Discount =25% of TotalPrice TotalPrice >=25000 and TotalPrice <50000 - Discount= 15% of TotalPrice TotalPrice <250000 - Discount =10% of TotalPrice Netprice= TotalPrice-Discount *Show( ) – to display Customer details. Implement the above functions in main(). 15 Define a class Play with the following specifications: Private members : Playcode integer PlayTitle 25 characters Duration float Noofscenes integer
Public member functions:
A constructor function to initialize Duration as 45 and NoofScenes as 5. A parameterized constructor function that assigns the values of Duration and Noofscenes with the help of corresponding values passed as parameters to this function. NewPlay() function to accept values for Playcode and PlayTitle. ShowPlay() function to display all the data members on the screen. THE END
Python Advanced Programming: The Guide to Learn Python Programming. Reference with Exercises and Samples About Dynamical Programming, Multithreading, Multiprocessing, Debugging, Testing and More
Python Advanced Programming: The Guide to Learn Python Programming. Reference with Exercises and Samples About Dynamical Programming, Multithreading, Multiprocessing, Debugging, Testing and More