OBJECT ORIENTED PROGRAMMING CIS 3100

Endl line feed operator causes a linefeed to be

This preview shows page 20 - 24 out of 54 pages.

endl Line feed operator : causes a linefeed to be inserted 8. setw Field width operator : provides a common field width for all the numbers and forces them to be printed right justified 6. Explain about Type conversions? There are three types of conversions. They are • Conversion from basic type to class type – done using constructor • Conversion from class type to basic type – done using a casting operator • Conversion from one class type to another – done using constructor or casting operator //TYPE CONVERSION FROM ONE OBJECT TO ANOTHER OBJECT #include<iostream.h> #include<conio.h> class sample { private: int val; public: sample(int a,int b,int c) { if((a>b)&&(a>c)) val=a; else if(b>c) val=b; else val=c; } int send() {
Image of page 20

Subscribe to view the full document.

return val; } }; class sample1 { private: int y; public: sample1() { } sample1(sample s1) { y=s1.send(); } void print() { cout<<"\n Greatest number is : "<<y; } }; void main() { int a,b,c; clrscr(); cout<<"\n Enter three numbers \n"; cin>>a>>b>>c; sample s1(a,b,c); sample1 s2; s2=s1; s2.print(); getch(); } 7. Explain inheritance? (Nov/Dec 2010) (Nov/Dec 2011) Inheritance is the process by which objects of one class acquire the properties of objects of another class. It supports the concept of hierarchical classification and provides the idea of reusability. The class which is inherited is known as the base or super class and class which is newly derived is known as the derived or sub class. The syntax of deriving a new class from an already existing class is given by, class derived-class : visibility-mode base-class { body of derived class } The various types of inheritance are, • Single inheritance : In single inheritance, one class is derived from an already existing base class. Here A is the base class and B is the derived class.
Image of page 21
• Multi-level inheritance : In multi-level inheritance, a new class is derived from a class already derived from the base class. • Multiple inheritance : In multiple inheritance, a single class is derived from more than one base class. • Hierarchical inheritance : In hierarchical inheritance, more than one class is derived from a single base class. • Hybrid inheritance : Hybrid inheritance is defined as a combination of more than one inheritance. Example : MULTIPLE INHERITANCE #include<iostream.h> #include<conio.h> class add { protected: int val; public: void sum(int a,int b) { val=a+b; } }; class sub { protected: int res; public: void minus(int a,int b) { res=a-b; } }; class mul:public add,public sub { private: int prod; public: void display() { cout<<"\n OUTPUT"; cout<<"\n~~~~~~~~"; cout<<"\n\n Added Value = "<<val; cout<<"\n\n Subtracted Value = "<<res; prod=val*res; cout<<"\n\n Product = "<<prod; } };
Image of page 22

Subscribe to view the full document.

void main() { clrscr(); int x,y; mul s; cout<<"\n Enter 2 numbers : \n"; cin>>x>>y; s.sum(x,y); s.minus(x,y); s.display(); getch(); } 8. Explain constructor and its types. (Nov/Dec 2011) A constructor is a special member function whose task is to initialize the objects of its class. It has the same name as the class. It gets invoked whenever an object is created to that class. It is called so since it constructs the values of data members of the class.
Image of page 23
Image of page 24

{[ snackBarMessage ]}

Get FREE access by uploading your study materials

Upload your study materials now and get free access to over 25 million documents.

Upload now for FREE access Or pay now for instant access
Christopher Reinemann
"Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

Ask a question for free

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern