OOP Classes & Objects - My2 PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document provides an overview of Object-Oriented Programming (OOP) concepts in C++. It explains classes, objects, structures in C and C++, and memory allocation for objects. The document is a suitable resource for those studying computer science or related topics, focusing specifically on object-oriented programming fundamentals.
Full Transcript
Classes & Objects Structures in C Example Disadvantages of structure Standard C does not treat structure as an built-in data type Suppose we have declared C1,C2,C3 as variables of above structure at that time we can not perform operation like C3=C1+C2 Structure do not permit data hiding. All...
Classes & Objects Structures in C Example Disadvantages of structure Standard C does not treat structure as an built-in data type Suppose we have declared C1,C2,C3 as variables of above structure at that time we can not perform operation like C3=C1+C2 Structure do not permit data hiding. All structure members are public members. Structures in C++ C++ support all the features as defined in C, but C++ has some expanded capabilities such as data hiding, Inheritance. In C++ a structure can have both function and variable as member(declaration) It also declare some members as private so that they cannot accessed directly by external function. C++ incorporates all these extension in What is Class in C++ A class is a way to bind data and its associated function together. It allows the data and function to be hidden, if necessary. When defining class we are creating new abstract data type that can be treated as other built-in data type Class specification has two parts: 1.Class declaration 2.Class function definition The class declaration describes the type and scope of its members, The class function definition describe how the class function are implemented The keyword class specifies, that what follows is an abstract data of type class_name Body of a class is enclosed within braces and terminated by semicolon. Class body contains declaration of variables and functions These variabes are collectively called as class members The keyword PUBLIC and PRIVATE are known as visibility labels If we don’t specify this label by default they are PRIVATE. Example Creating Object Syntax Class_name object_name; Example: item x; OR item x,y,z //multiple object We can also create object after class declaration as we do for structures(i.e declaring after semicolon) Defining member function Member function can be defined in two places” 1.Outside the class definition 2.Inside the class definition The function should perform the same task irrespective of the place of definition. Outside definition: An important difference between a member function and normal function is that a member function incorporates a membership ‘identity label’ in the header i.e class_name:: This ‘label’ tells the compiler which class the function belongs to. Syntax Example The member function have special characteristics that are often used in the program development. Characteristics are: Several different classes can use the same function name. the membership label will resolve their scope. Member function can access private data of the class The member function can call another member function directly, without using the dot operator Inside class definition: Another method of defining member function is to replace the function declaration by the actual function definition. When function is defined inside class is called as inline function Normally small functions are defined inside a class. Example An inline function is one in which the function code replaces the function call directly. Inline class member functions if they are defined as part of the class definition, implicit if they are defined outside of the class definition, explicit, I.e.using the keyword, inline. Inline functions should be short (preferable one-liners). Why? Because the use of inline function results in duplication of the code of the function for each invocation of the inline function class CStr { char *pData; int nLength; Inline functions within class … public: declarations … char *get_Data(void) {return pData; }//implicit inline //function int getlength(void); … }; inline void CStr::getlength(void) //explicit inline function { } return nLength; Inline functions outside of class … declarations int main(void) { char *s; int n; In both cases, the compiler will CStr a(“Joe”); s = a.get_Data(); insert the code of the functions n = b.getlength(); } get_Data() and getlength() instead of generating calls to these functions Making outside function inline: when we define member function outside the class definition and still make it inline by just using the qualifier inline in the header line of function definition Memory allocation for object The memory space for object is allocated when they are declared and not when the class is specified. This is only partly true the member funaction are created and placed in the memory space olny once when they are defined as a part of class specification Since all the objects belonging to that class use the same member function,no separate space is allocated for member functions when the objects are created