0% found this document useful (0 votes)
58 views

Object Oriented Programming: UNIT 1-Revision (16 Marks)

Object Oriented Programming concepts like classes, objects, methods, encapsulation, inheritance, and polymorphism are discussed. The key differences between classes and structures in C++ are explained with an example. Methods of data hiding like private, protected, and public access specifiers are described. Constant and volatile functions are also covered.

Uploaded by

Prabha Bala
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Object Oriented Programming: UNIT 1-Revision (16 Marks)

Object Oriented Programming concepts like classes, objects, methods, encapsulation, inheritance, and polymorphism are discussed. The key differences between classes and structures in C++ are explained with an example. Methods of data hiding like private, protected, and public access specifiers are described. Constant and volatile functions are also covered.

Uploaded by

Prabha Bala
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Object Oriented Programming

UNIT 1- Revision (16 Marks)

1. Explain the basic concepts of Object oriented programming


Objects Classes Methods And Messages Abstraction Encapsulation Inheritance Abstract Classes Polymorphism.

Message Passing Dynamic Binding

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

Access Base class Derived classes Outside classes

Public Yes Yes Yes

Protected Yes Yes No

Private Yes No No

11

4.Write a C++ program using inline function


Inline function is expanded in line when it is invoked. The compiler replaces the function call with the corresponding function code. Syntax: 1. datatype function(parameters) { statements; } 2. inline datatype class::function (parameters) { statements; }

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 ( ); }

5.Write a C++ program to illustrate the static function

13

6.Explain about call by value,reference and return by reference with program.


Call by value: The value is passed in the arguments when function is called. The called function creates a new set of variables and copies the values of arguments into them. The function does not have access to the actual variables in the calling function and can only work on the copies of values.

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.

7. Explain briefly about function overloading with a suitable example

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

p gets the address of x in memory.

What is a pointer?
int x = 10; int *p;

p = &x;
*p = 20;

20

*p is the value at the address p.

What is a pointer?
Declares a pointer to an integer

int x = 10; int *p = NULL; p = &x; *p = 20;


& is address operator gets address of x

* dereference operator gets value at p

Pointers to Member Operators


.* operator
It is used to access a member of a class using a pointer to it by an object. <objectname> .* <pointername> Ex: int stud::*rollpt=&stud::rollnum; //rollpt = rollnum stud newstud;// new object creation newstud .* rollpt = 2; // new student 2 is created

* 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;

9.Explain Nested classes and local classes with an example


LOCAL CLASSES
The classes that are defined inside a function is known as Local Classes. Ex: void fn_name ( ) { .. class class_name ( ) { .. }; }

NESTED CLASSES
Class within another class is known as Nested Class. Ex: class <class_name_out> { .. class <class_name_in> { }; };

10.Discuss constant and volatile functions in detail


CONSTANT FUNCTION If a member function does not alter an data in a class, then we may declare it as a const member function.
return_type fn_name (args) const { function body; }

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; }

You might also like