Object Oriented Programming: UNIT 1-Revision (16 Marks)
Object Oriented Programming: UNIT 1-Revision (16 Marks)
2.Difference between class and struct and also illustrate with an example
A class is similar to the Structure concept in C. It is a new way of creating and implementing a userdefined data type. Ex: C structure: C++ Class: struct stud | class stud { | { char name[20]; | char name[20]; int roll_num; | int roll_num; float total_marks; | float total_marks; }; | };
3
C Declaration: | C++ Declaration struct stud A; | stud A; Specifying a Class: It is to bind the data and its associate functions together. It allows the data to be hidden. A class specification has two parts:
Class Declaration Class function definition
Class Declaration: class class_name { private: variable declaration; function declaration; public: variable declaration; function declaration; };
Ex: class abc { int number; float cost; public: void getdata(int a, float b); void putdata(void); };
Creating Objects: item x; // x is an object for class item item x,y,z; // more than one object can be defined. //At this stage necessary space is allotted // for an object. class item { } x, y, z; // This is also possible.
Accessing class members: object_name . function_name(actual_arguments); Ex: x.getdata(100,75.5); // message x.putdata(); // message
getdata(100,75.5); // has no meaning x.number=100; // illegal
Class Function Definition: The member functions can be defined in two ways: 1. Outside the class definition 2. Inside the class definition Outside the class definition: return_type class_name :: function_name (arg) { function body }
The class_name :: tells the compiler that the function_name belongs to this class. :: is called as scope resolution operator. Ex:
void item :: getdata (int a, float b) { number = a; cost = b; } void item :: putdata (void) { cout << Number : << number; cout << Cost : << cost; }
3) Explain briefly the Methods of Data Class person hiding: { class person
{ private: int age; //private data int getage( ): //private function }; person p1; a = p1.age;//cannot access private data //error p1.getage( ); //Error access protected: int age; // protected data int getage( ): //protected function }; person p1; // Cannot access protected members a = p1.age; p1.getage( ); } class person { public: int age; //public data int getage( ); //public function }; person p1; a = p1.age; // can access public data p1.getage( ); // can access public function
Private Yes No No
11
Like static member variable, we can also have static member functions. A static function can have access to only other static members declared in the same class. A static function can be called using the class name. Ex:
class test { .. static void showcount (void) { cout << count = << count; } . }; main ( ) { .. test : : showcount ( ); }
13
14
Ex: class one { public: void swap(int n1, int n2) { int t; t=n1; n1=n2; n2=t; cout<<n1<<" "<<n2; } };
void main() {
int num1,num2;
one obj; cout<<"Enter 2 numbers:"; cin>>num1>>num2; obj.swap(num1,num2);
15
Call by reference:
Here we pass parameters to the functions by reference. The formal arguments in the called function become aliases to the actual arguments in the calling function. The function is working with its own arguments, it is actually working on the original data. Ex: class one void main()
{ public: void swap(int *n1, int *n2) { int t; t=*n1; *n1=*n2; *n2=t; } }; { int num1,num2; one obj; cout<<"Enter 2 numbers:"; cin>>num1>>num2; obj.swap(&num1,&num2); }
16
Return by reference: A function can also return a reference. In our example, max(a,b)=-1; // assigns -1 to a if a is greater otherwise -1 to b. Ex:
class one { public: int &max(int &x, int &y) { if(x>y) return x; else return y; } };
17
Overloading refers to the use of the same thing for different purposes. We can use the same function name to create functions that perform a variety of different tasks. It is also known as Function Polymorphism.
Ex: Declaration:
int add (int a, int b); int add (int a, int b, int c); double add (double a, double b); Function Calls: cout << add (5,10); cout << add (10.2, 7.5); cout << add (3, 7, 9);
18
8.Explain the use of constant pointers and pointers to constant with an example
Pointers and objects int x = 10; p int *p;
p = &x;
10
What is a pointer?
int x = 10; int *p;
p = &x;
*p = 20;
20
What is a pointer?
Declares a pointer to an integer
* operator It is used to point a member of a class but dont contain address. It contains only the offset into the class where a member can be found. Ex: stud *newstudpt=&newstud; newstudpt * rollpt = 3;
NESTED CLASSES
Class within another class is known as Nested Class. Ex: class <class_name_out> { .. class <class_name_in> { }; };
VOLATILE FUNCTION
A member function can also be declared as volatile if it is invoked by a volatile object. An volatile objects value can be changed by external parameters which are not under the control of the program. return_type fn_name (args) volatile { function body; }