Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES.

Similar presentations


Presentation on theme: "Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES."— Presentation transcript:

1 Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES

2 Objectives Upon completion of today lecture, You will be :-
Able to understand the basic concept of object oriented programming. Able to understand Class , Objects, Class Members, Access Control, Parameter Passing. Able to use function inside and outside the class. Able to understand the functions with Parameter Passing

3 Structures ( revision)
Keyword Name of Structure A way to combine different types of data into a single unit with continuous memory location Tool for handling group of logically related data items struct student { char name[20]; int roll_num; three fields with diff. data types- float total_marks; structure members or elements }; Three fields with diff. data types-structure members or elements

4 Structures Revised Keyword Name of Structure
Structure name (student) can be used to create variable of type student. Example: struct student A; // C declaration Dot opertaor is used for accessing member variables as: strcpy(A.name, “John”); A.roll_num =25; A.total_marks =550; final_result = A.total_marks + 20; Structures can have array, pointers or structures as members. Declaration of a structure type variable Assigning value to member variables

5 Structures Revised Limitations of C structures: Struct data type can not be treated as built-in types, as shown: struct sum { float x; float y; }; struct sum a, b, c; Values can easily be assigned to a, b, c using dot operator but we can not add or subtract one from other, as shown: a= b+c; // illegal in C Secondly, no data hiding in structures i.e. structure members can be directly accessed by other structure variables by any function anywhere in their scope. Means structure members are public members.

6 Extensions to Structure
C++ supports all the features of structures in C but expanded its capabilities to suits OOP philosophy User-defined data types to be treated as close as possible to the built-in data types Feature of DATA HIDING (one of the main principles of OOP) In C++, both variables and functions be used as members Some members can be declared as private so that can not be accessed by others directly. Keyword struct can be omitted in the declaration of structure variable, example: student A; // legal in C++ but illegal in C

7 Extensions to Structure
C++ includes all these extensions in another user-defined type called class. Small syntactical difference b/w structures and classes in C++, hence one can use both interchangeably with minor modifications. Most of the programmers use structures for holding data and classes for both data and functions. The only difference between structure and class in C++ is that, by default, members of class is private while public in case of structure.

8 Class declaration- describes the type and scope of its members
A way to bind data and functions together and allows data to be hidden, if required, from external use While defining class, we are creating a new abstract data type that can be treated as any other built-in data type. Two parts of class specification: Class declaration- describes the type and scope of its members Class function definitions- describes how the class functions are implemented.

9 General declaration of Class
Keyword General declaration of Class Name of a Class class class_name { private: variable declarations; function declarations; public: }; Data hiding Access Specifier Class members

10 Class Variable declared inside class are- data members and function are- member functions. Only member functions can have access to the private data members and private functions Binding of data and functions together into a single class-type variable is referred to as encapsulation.

11 Class Simple example: class item { int number; //variables declaration
float cost; //private by default public: void getdata(int a, float b); // function declaration void putdata(void); };

12 Object Creation item z; // memory for z is created
Remember- class declaration does not define any object of class but only specifies what they contain Once class is declared, we can create variable of that type by using the class name, say item z; // memory for z is created Here the class variables are known as objects, so z is object of type item One can declare more than one object in one statement, say- item a, b, c; Also possible way, like in structures, for creating objects is: Class item { ……. } a, b, c;

13 Accessing Class Members
The main() cannot contain statements that access data members directly. They can only be accessed through member functions. Syntax for accessing class members- object-name.function-name(actual-arguments); Example: x.getdata(500, 42.5); // is valid for object z Similarly- x.putdata(); //would display values

14 Accessing Class Members
Invalid statements- getdata(100, 42.5); x.number= 300; //such statements have no meaning Variable declared as public can be accessed by objects directly, as shown: class xyz { int x; int y; public: int z; }; ……… xyz p; p.x =10; //error, x is private p.z =20 // accepted, z is public, should be avoided (defeats idea of data hiding)

15 Defining Member function
Two places: Outside the class definition Inside the class definition Irrespective of the place of function definition, the function should perform the same task.

16 Outside the Class Definition
Difference b/w member function and normal function is that a member function incorporates a membership ‘identical label’ in the header. This ‘label’ tells the compiler which class the function belongs to. Member function definition: return-type class-name :: function-name (argument declaration) { Function body }

17 Outside the Class Definition
:: scope resolution operator, tells the compiler that the function function-name belongs to the class class-name. This means, scope of the function is restricted to the class-name specified in the header line. Void item :: getdata( int a, float b) { Number = a; Cost = b; } Void item :: putdata(void) Cout<< “number :”<< number << “\n”; Cout<< “cost :”<< cost << “\n”; }

18 Outside the Class Definition
Characteristics of member functions: Several different classes can use the same function name. Member functions can access the private data of the class. A member function can call another member function directly, without using the dot operator.

19 Inside the Class Definition
Example: class item { int number; Float cost; Public: Void getdata(int a, float b); Void putdata(void) //treated as inline function Cout << number << “\n”; Cout << cost << “\n”; } };

20 Inside the Class Definition
Function defined inside class is treated as inline function. Hence all restrictions and limitations that apply to an inline function are also applicable here. Only small functions are defined inside the class definition.

21 C++ with class Int number; Float cost; Public:
# include <iostream.h> Class item { Int number; Float cost; Public: Void getdata(int a, float b); Void putdata(void) Cout<<“number :” <<number<< “\n”; Cout<<“cost :” <<cost<< “\n”; } }; Void item :: getdata(int a, float b) Number=a; Cost=b;

22 C++ with class int main () { item x; cout<< “\nobject x” << “\n”; x.getdata (200, ); x.putdata(); item y; cout<< “\nobject y” << “\n”; y.getdata (700, ); y.putdata(); return 0; }

23 # include<iostream.h> class set { int m, n; public:
When a member function can be called by using its name inside another member function of the same class, it is known as nesting of member function. # include<iostream.h> class set { int m, n; public: void input (void); void display (void); int largest(void); }; int set:: largest (void) { if (m >= n) return (m); else return (n); } void set : : input (void) cout << “input values of m & n:”;

24 Nesting of member functions
int set:: largest (void) { if (m >= n) return (m); else return (n); } void set : : input (void) cout << “input values of m & n:”; cin >> m >> n; void set :: display(void) { cout << “largest value = “ <<largest(); //calling member function } main() set a a.input(); a.display();


Download ppt "Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES."

Similar presentations


Ads by Google