40% found this document useful (5 votes)
4K views84 pages

Oop Decode

Uploaded by

Piyush Soni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
40% found this document useful (5 votes)
4K views84 pages

Oop Decode

Uploaded by

Piyush Soni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 84
Q.2 Explain the need of object or [unie 1 | ne Chepter- 6 Standard Template Library (67) To TBO} troduction to STL. 6-1 6.2 STL Components... 6-1 6.3 Sequence Container 6-2 6.4 Associative Container. 6-4 6.5 Container Adapter. 6-6 8.6 AlgOrItAME rasan 6-11 6.7 Set Operations ecu ‘ 8-15 6.8 Heap Sort . 6-16 6.9 Iterators - input, Output, Forward, Bidirection and Random Access nen = 18 6.10 Object Oriented Programming - A Road Map to Future ..6 -20 Solved Model Question Paper (M- 1) to (M2) Solved SPPU Question Paper (S-1toS-6) ) |. Emphasis son data rather than Fundamentals of Object Oriented Programming 1.1 : Introduction to Procedural, Modular, Generic and Object Oriented Programming Techniques 6, Some common properties a 1.3 + Funds 2.3 Explain the features of ob (ea (SPU bay 37 .1 Compare procedure ot Programming. Ans. : obje 8 (SPPU : Dec.-i9, Nay. Se. No. ‘Object Oriented Programming Language (op) Procedural Programming | Ana: 4. Object Language + Object is am instance of a « + Opler are baste suri, ~ + Tm Ges the class variables ‘sess membervariah + Example rae ft: Forte las Frat the object Ths major foeas i on procedures The sain Tous ls on objec asses : USIEISSTERRE 5 functions. 2c The (2) Pe ety st joie. Dane aang p mcatae | EE important feature of OOF. ara ie 2, This is a top down programming Tis approach, bottom up Programming approach, Data biding can be done by private: int len, Be, simple to implement. 11 is complex to implement | For example : C, Foran, For example = C++, JAVA coBoL, cod PE a eh tae | 1.2 : Need of Object Oriented Programming 22 Explain the need of ot 4 peosramming Fundamentals of Object Oriented Programming ‘duction to Procedural, Modular, Generic Sct Oriented Programming Techniques fedure erlented programming ve, object em (SPP bes ‘ral Programming bj riebted Language Programming Language (oP) ——___ f00P)" lua programing ‘des of procedures Drosranmlng aproech, ean ison proceakzes pe 1 isnot posi, abit is one of the importa: fears 0f OOP, pon [et possible. "Daca Sling san be done by tmakiog petra. implement “A Glee Engewing Ses ot sQhim js a abise {SPIEL iH One Gr ex fer ‘Sauer raw) resize Fig. @.3.2 Hierarchical structure of inheritance a What ig the difference between data encapeu abstraction ? Ans, So) Data excaprulaion No. : 1. This a process of binding data members of a class to the member fnetions ofthat class. Data abstraction 2 = Data encapsulation depends upon “g, object data type. It is used in software implementation phase Data encapsulation can be achieved by inheritance, ———— “A Guide for Engineering Stes jet OentedProgramming_2°5 ect Orit Prepon ject rented Programming _1-5_Ott Orient 14 : Benefits of OOP 2.5 What are the benefits of OOP ? : joing ae some advantages of object oriented ce the redundant code can be cl can be used og modules can be crested using ye modules can then communicat \2eDue to data hiding propery, important data can be kept aay & smauthorized access 4. Its possible to creste multiple objects for a given class. 5. For upgrading the system ffom small scale to large scale is poss due to object oriented fearure. 6. Due to data centered nature of object oriented programming mos the deuals of the application model can be captured 7. Message passing technique in object oriented programming allows be objects to communicate to the external systems. 8 ng the code for simplicity, understanding and debuseios ible due to Q8 What is the difference between C and C++? Ans. C language C isa procedure oxented language 2 | C makes approach of Soest 1 Guiee for Engincerng Ste {int main) q ented programming - + eliminated and the cout <"Hello World\n' > using object oriented fate t0 each other to ‘28 What are primitive data types and user 1 be kept away from lass. ge scale is possible tentals of gramming Object Ortente Programming 1-7 1.6 : C++ Programming Basics deunming most of | 0.7 Give the structure of Cre a Ans.: There are four sections in the structure of C++ progtam - | | t Cet Oriented Programming o Ans. : Definition : Aapieet Fundamentels of beet Oriented Program, oie aures with sultable example ucture. Thus: sty i cllection of Various dsta item: ‘The Syntax of declaring steucture in Cry is as follows struct name { member 1; member 2; member n; h Example : struct stud { {int roll_no; | The stud 1 and stud 2 will look like this char name[10j; | [~~ float marks; b struct stud stud1,stud2; Q.10 Write a C program to represent a complex number using structure and add"two complex numbers. Ans. #include using namespace std; typedef strict Complex { float real; float img; Fat Object Orented Programming 179 Object Orient ject Oriente vvold main) : a om enum ne couch art of Sst complex number. For exat eee aces ‘ pee ee ee ee reac out Oatput ‘The string : Holo ‘The copied string i: Hebe | the now string is: Friends veld mating) ‘The concatenated string ie HolloFriends Pinctude ‘using sameepace std 1.13 : Class and Object - 48 Define the term class. a [SPPU May-26, Marks 2} Ans, : * Each classiis/a/collection of data and functions’ that manipulate the data, ¢ Placing the data and functions together into a single entity is the central idea in object oriented programming The nouns in the system specification help the C++ pro determine the set of classes. The objects for these clas These objects work together to implement the system, ‘* Classes in C++ is the natural evolution of C notion of struct. aeons 14 Guide for Engineering Saudens beet Oriented Programming 1-18 Fundamentals of Object Oriented Programming For exam Object Oriented Programming 1-19 | class rectangle i t private: Int leabr, Public vold get_datag); vold aread) wold print_datag; k Q.49 Give the difference between structure and class Ans. = i ee ee IEE Sr. Structure es ‘Class. | No. | By default the members of structure | BY default the members of clase ate | are public. private, 2, The structure cam not be inherited. ‘The class can be inberited, i 7 3. The structures do mot require The’ classes require constructors constructors, for intialiaing the objects. 4, A structure contains omly data Alas cautains the data as well as| members. she fu LaZ6 What is class and object ? Differentiate between class and \ object. : 1 [SPPU # Dee-37, Mars 6) Ans, : Class : Refer Q18, Object : object interacts with the help of Procedures or the methods defined within the class. | aes “Goer Engnerng Sede Furdament Object Ortented Progra Difference between Class and Object [Sr No. Class y Object Narmada can be the objects of belonging class the class River. pe of the class is | The object can be Throughout the program. destroyed a5 requizements, ‘The class can not be initialized with some property values Various" objec taviog names cen be created fo same class 1.14 : Class and Data Abstraction Q.21 How does class play an important role in data abstraction ? Explain jbstraction is one of the characteristics of C++ by which 4 representation of the C++ program is presented to the user. (2) The implementation details are hidden. (3) Class is used to achieve data abstraction in C+. (4) Example class Student 4 Private: int roll; char name[10]; public: void input) void display(); LN Se Gees Guide for Engineering Stren Fundamentals of Object Oriented Programming 1-20 Object Oriented Programming % In the main function, we can access the functionalities using the object. For instance - iden 1.15 : Class Scope and Accessing Class Members Q22 Explain the method of accessing class members used in Cé+ with the help of suitable example, 3 class has two types of members - i) Data members and ii) ‘member functions. (2) Typically the data members are declared as private and member Aumetions are declared as public. Hence the data members of class are ‘accessed used by member functions of a class. G) For accessing the class members outside the class, the object of that class is created and used. (4) The member functions can be defined inside a class or outside a class. For defining the member functions outside the class the scope resolution operator is used, (S) Example = #include using namespace std; class Test { private: Data members aro pees declared as private. public: int Addition(int a, int b) { c= ath; return 6; the function te data ‘members can be manipulated, eons A Gulde for Engineering Students Object Oriented Programming 1-21 Object Ori ) void Display); k void Tost:Display(.)//Using Scope resolution operator { //aieplay function is defined outside the clan cout << "The gum is:" << ¢ << "\n' ) int main() 4 ea (Only member functions are accessinie outsise se laze because they are public. | 2.24 Explain the terms private, public and protected, ‘Ans. : © By default the access specifier is of private type data and functions are declared as private then only members class can access them. This achieves the data hiding property Public access specifier allows the function declared outside she lass to access the data and functions declared under it ‘+ The protected access specifier allows the functions and data declared under it be accessible by the belonging class and the immediate derived class. Outside the belonging class and derived class these members art not accessible. —— A Guide for Engineering Smdess Greene "| = | tunaamentats oy Object Oriented Programming 1-22 Object Oriented Programming Object Oriented Programming 1-23 Fandamentels of Object Ortented Programming Thus the C++ program cf © Header files - cont: definitions and function prototypes © Source-code files - contains member function definitions The advantage of this arrangement is that the modification of the program becomes easy. 1.18 : Functions Q.26 What is function prototype ? Ans.: A function prototype is declaration of function without ~ specifying the function body. That is the function prototype specifies name of function, argument type and retum type. Q.27 Explain call by value and call by reference with sultable examples : 1. Call By Value : In this method, actual values are passed as parameters to the function. For example void sum(int x,int y)/*definition*/ { int ¢: caxty; cout<<"\n The Addition is: "< include using namespace std; class TRAIN { protected: cout << "\n Enter trainno: *; cin >> trainno; cout << "\n Enter destination: *; cin >> dost; cout << “\n Enter distance: "; cin >> distance; : + void show() { cout << "\n Train No: " << trainno; cout << “\n Destination : "<< dest; cout << "\n Distance :" << distance; + void put() _—_// Member function for write file ee ‘A Guide for Enginesting Students object Oriented Programming Fang 1-35 Object Oriented to file y X int main) { TRAIN obj; i cout << *\aEnter the Record"; 1.19 : Accessing Function and Utility Function 2.28 Explain the concept of access and utility function: Ans. : + Access Function © Itis a fimetion that can read or display data © Another use of access function is to check the tuth cr fit jons. Such functions are also called as predicate functions mnetion Iper or supporting fiction that can be used by = to perform some common activities. © It is a private function because it is not intended to use out class, double division(double a, double b)//Utility Function 1 c=a/b; Tonia jor Ergon qecones ide lo Object Oriented Programming 1-26 Object Oriented Prope ‘Goceeecmaaaaunannea terrae geet (double x, doubley) // Access function constructors. 4. Tie |/eonstnictor’stotild not have any/return type. Even a void type should not be written for the constructor. 5, The constructor can not be used as a member of union or structure. 6. The constructors can have default arguments. \- 632 Write a C++ program for parameterized constructor. ‘Ans, : C+ Program #include using namespace std; class image 4 private — Secon A Gulde for Engineering Sudents IESE ''Z Object Oriented Programming 1-27 at Grin ei of ew SS nee rein int height, width: public: Smage(int xt) //oonstructor 4 height=x; width=¥; ’ int area() { retum (height-width); y k int main() q image obj1(6,2); cout<<"The area is :"< ‘using namespace std; class Arithmetic { int d1.d2; char op; public: Fundamentals of Object Oriented Programming 1-28 Object Oriented Programming Asithmetic(int x.char e.int y) { di=x, dary; opec; case /:0=d1/a2; break; cout<<"\n Addition of 10+20= "< using namespace std; class image height=new int//assigns memory dynamically using ‘new image::~image() « delete height;//destroys the memory using ‘delete! delete width; ) init tmage:sarea() { Tetum (*height* *width); + int main) 4 image obj1(10,20); cout<<"The area is :"< ‘using namespace std void fund) 4 atic int ent=0; cout << "\ncount =" < ++ Program without using ‘tatle ‘include tnt ent=0; ‘cout << \ncount = * < {inline largest(int x,int y,int 2) flx> y&e8ox>2) 4 cout<-<* First number is greatest and itis: "<< } else iffy>sxdeBey>2) t ‘cout<<" Second number is greatest and itis: "<>x; cout <<"\n Enter the value of y*; ein>>y: /hasing 0b of derived class base class member is accessed obj.sot_y obj.show_x0; // access member of B obj.show_y(); // access member of derived clase Object Oriented Programming 2-3 Inheritance and Pointere ? output Enter the value of x90 Enter the value of y70 x= a0 y= 70 In above program the obj is an object of derived class. Using obj we are ‘member function of base class, The derived class inherits base class using an access specifier public. G4 Write « C++ program to Inherit base class in private mode. Ans. : include using namespace std class Base { // Yohertt as private class derived : private Base ¢ int y; public: void set_y(int n) a 7 “Gt for Engncng Sane ject Oneres Programming 2-4 __A rag 4 yen > void show.¥0) { cout <<"\n y= "<<¥i > % tnt main() ( derived obji//object of derived class nt x : cout <"\a Enter the value of oin> > 2. Extensib cout<<"\n Enter the value of y": oe As indicated by the comments the above program will generate ens ‘messages “not accessible”. This is because the derived class inherts base class privately. Hence the public members of base class beat private to derived class 2.4 : Protected Members EE Explain why and when do we use protected instead of pit? Sar SPU tees = _——___—_— “A Gul for Engineering St objet Oriented Programming Inheritance and Pointers but the derived-derived class members are prohibited to class members. 1, Reusability : The base class code can be used by derived class need to rewrite the code, 3. Data hiding : Base class can decide to keep some data private so thet it cannot be altered by the derived class. 4. Overriding : With inheritance, we will be able to override the methods of the base class so that meaningful implementation of the base class method can be designed in the derived cless. 2.6 : Constructor and Destructor in Derived Class @.7 Explain the execution process of constructors and destructors in derived class. Ans. : + When we create an object for derived class then fist of all the Base class constructor is called and after that the Derived class constructor is called. SS ‘A Gulde for Engineering Sudents | Object Oriented Programming 2-6 Inheritance and Pointers in function finishes running, the derived class's desrscioe first and after that the Base class destructor will be led as chain of constructor calls. publ Base() (cout << "Base constructor” << endl; } ~Baso() (cout << "Base destructor” << endl; } hk class Derived:public Base { public: Derived) {cout << “Derived constructor” << end } ~Derived () {cout << “Derived destructor” << endl: } b void main() { Derived obj; } ‘The output of above code will be invoking of base class constructer, then derived class constructor, then derived class destructor and finally base destructor. 2.7 : Overriding Member Functions ept with suitable example Q8 Explain the function overriding conc ‘Ans.: Definition : Redefining a function in a derived class fun ding. For example - Consider following C-+ program that wses the sine fanetion name i.e. print_msg in base class and derived class. The "a Guide for Engineering Suen Object Oriented Programming 2-7 void got_megd) { a=10; be20, » void print_meg() 4 e=a+bi//performing addition cout<<"\n C(10+20)= "<<; cout<<"\n Tim print msg() in class A’; } 8B: public A. { private: int ab public: void set_mag() { a= 100; bet0; = Gaara oeng mea > old main) A obj_base: ‘obj derived.sez_mag ss obj derived print_msg()//but diferent tasks we program will display 30 due to execution of print mg of base class and 90 due to execution of print_msg function o derived class PHO] [Teaching student 3 = 7 ie for Emig Se Object Oriented Programming 2-9 The implementation of class hierarchy Inheritance. 2.9 : Types of Inheritance 2.10 With suitable examples, explain different typ Morwrite a note on type - single, mu ‘Ans. : Various types of inhe 2, Multilevel inheritance 3. M\ In single inheritance one child class is derived fom one base class. Winelude ‘using namespace sta: clase Base public: void set_x(int 2) } void show x) cout << "nltx =" << x ) , class derived : public Base { int pubue: (obj. show_xy0; // access member of derived class = ae Fig. 0.10.2 Mutilevel inheritance —_——— ee 2, Multilevel inheritance Bt is a kind of inheritance in which the derived class is derived. from | single base class which itself is 0 — Seco “A Gude for Engineering Students Object Oriented Programming 2-11 Inheritance and Pointers {cout<< "\n The value of x is "<< x; } » class B:public A void put_b0) {cout<<"\n The value of y is "<getarea(): rotum 0; » ‘The above program will display area of square as 200 and area of tianale classes ? Write a program having student id create many derived. classes’ auch ot ‘te. from the student ‘#include ‘#include = using namespace std; class Stuciont { char name[10}; public: oid SeNametcbar nto) strepy(name.n) i} void GetName(char nj10}) { strepy(n.name); } virtual void qualifcation()=0; class Engg:public Student { public: ’ void qualiscation() { char GotName cout< {nt maing { Student“; Engg 6; ->qualifcation(); cout<<"\n Enter the name: "<->nm Output Enter the name: Ramesh Ramesh is a an engineering student Enter the name: 2.13 : Friend Class QE Explain the friend class concept. Similar to a friend function one can declare a class asa friend t0 fanother class. This allows the friend class to access the private data ‘members of the another class. —_—_———— exes “Ge or Engnerng Sader Object Oriented Programming 2-25 Inheritance and Pointers For example clase A 4 onject Oriented Programming ‘Tne program for demons Siend class B://clase B is friend of class A public: fincas Al//eonstructor ose oe = : { a dats = 5 heats % int a; // Note that this member is public a class inner = clase B t oe ; public . = void fun(outer *o,int val) public: a“ Fo: {nt subtint 2) ope = vai = { cout<<‘a= "<a; Ni ] A obj; //objoct of class & 3 {ihe private data of class A is accessed in class B q 1/ data contains § and x contains 2 rotumn obji.data ~ x; m } K ) outer obj; © outersinner obj2; 2.14 : Nested Class = obj2.fun(S0bj1,10); //invoking the function of inner class LAZTG What Is nested class ? Write a C++ program to demonstrate the concept of nested class. } Ans. : When one class is defined inside the other class then it is called ie the, nested class, The nested class can access the data member of the Output outside class. Similarly the data member of the nested can be accessed a= 10 from the miain, Following is a simple C++ program that illustrates the use of nested class. a < —— ss A Guide for Engineering Students Ss "A Gul for Eg se a a a ip maents Inheritance and Pointers ted Programming 2-25 represents the memory location of some other variable, The purpose of pointer is to hold the memory location and not the actual value. For example « toring some value in a*/ jetting value from address in ptr and storing it in b*/ 2.16 : Memory Management : New and Delete lain the purpose of new and delete operators with suitable ‘The dynamic memory allocation is done using an operator new. int *p: penew int; locate the memory for more than one clement. For instance if locate memory of size in for 5 elements we can declare For example ints}; ‘The memory can be deallocated using the delete operator. For example dotote p: C++ Program using new and delete operators int main () { {nt in; int *p; ~ cout << “How many numbers would you like to type? ": cin >> P= new int[ij//dynamic memory allocation Hf (p == 0) Goes ‘A Galde for Engineering Soudents Inheritance and Potters Object Onented Programming 2-26 cout << "Error: memory could net be allocated"; tse for (n=0; nck: n+) « cout << “Enter The Number: " in >> plat for (n=0; neti n+ <<" <getVall)<

You might also like