0% found this document useful (0 votes)
2K views

Classes & Object: Member Access Determines If A Class Member Is Accessible in An Expression or

The document discusses classes and objects in C++. It explains that a class defines a new user-defined data type that can contain data members and member functions. Classes allow for data encapsulation through different access specifiers like public, private, and protected. Objects are instances of a class that allocate memory and can access class members. Nested classes define a class within another class and have restricted scope. Inline functions are functions that have their definition included in the calling code instead of just the declaration. This avoids the overhead of a function call by substituting the function body directly into the code.

Uploaded by

Niti Arora
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

Classes & Object: Member Access Determines If A Class Member Is Accessible in An Expression or

The document discusses classes and objects in C++. It explains that a class defines a new user-defined data type that can contain data members and member functions. Classes allow for data encapsulation through different access specifiers like public, private, and protected. Objects are instances of a class that allocate memory and can access class members. Nested classes define a class within another class and have restricted scope. Inline functions are functions that have their definition included in the calling code instead of just the declaration. This avoids the overhead of a function call by substituting the function body directly into the code.

Uploaded by

Niti Arora
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Classes & Object

A class is a mechanism for creating user-defined data types. It is similar to the C language structure data type. In C, a structure is composed of a set of data members. In C++, a class type is like a C structure, except that a class is composed of a set of data members and a set of operations that can be performed on the class. In C++, a class type can be declared with the keywords union, struct, or class. Structure and class objects hold a complete set of members. Each class type represents a unique set of class members including data members, member functions, and other type names. The default access for members depends on the class key:

The members of a class declared with the keyword class are private by default. A class is inherited privately by default. The members of a class declared with the keyword struct are public by default. A structure is inherited publicly by default.

Once you create a class type, you can declare one or more objects of that class type. For example: class X { /* define class members here */ }; int main() { X xobject1; // create an object of class type X X xobject2; // create another object of class type X } Member access determines if a class member is accessible in an expression or declaration. Suppose x is a member of class A. Class member x can be declared to have one of the following levels of accessibility:

public: x can be used anywhere without the access restrictions defined by private or protected. private: x can be used only by the members and friends of class A. protected: x can be used only by the members and friends of class A, and the members and friends of classes derived from class A.

Members of classes declared with the keyword class are private by default. Members of classes declared with the keyword struct are public by default. To control the access of a class member, you use one of the access specifiers public, private, or protected as a label in a class member list.

class A { int a; void f1(); protected: int b; void f2(); public: int c; void f3(); friend void fn(); }; The following table lists the access of data members A::a, A::b, and A::c in various scopes of the above example. Scope function f1() function f2 () Function f3() Friend Function fn() Object in any function object passed to another function A::a Access Access. Access. Access. No access. No access. A::b Access. Access. Access. Access. No Access. No Access. A::c Access. Access Access Access access access

An access specifier specifies the accessibility of members that follow it until the next access specifier or until the end of the class definition. You can use any number of access specifiers in any order. If you later define a class member within its class

definition, its access specification must be the same as its declaration. The following example demonstrates this: class A { : : public: : : };

All the members will be private All the members will be public

Nested Classes- When a class is defined within another class, it is known as nested class. By defining one class within another the scope of the inner class is restricted to outer class. The term nested class is restricted to its surrounding class. Except that you use the term like object, references, explicit pointers, names, declarations in a nested class can only use noticeable constructs, which includes static members, and enumerators from the encircling class and global variables. #include<iostream.h> Date is a nested class of class person main class person { char name[20]; class date { int dd,mm,yy; public: void get_date() { cout<<enter date(dd\mm\yy); cin>>dd>>mm>>yy; } void display { cout<<dd<<mm<<yy; }birth_date; } Public: void get_data() Object of nested class is declared { cout<<enter name of person; in main class to access the data cin>>name; cout<<enter birth date; birth_date.get_date(); } void show() Calling member function of nested { class in main class to cout<<name<<name; access the data cout<<birth date ; member of nested birth_date.display(); class }

} main() { Person p; p.get_data(); p.show(); } Name 20 Birth_date dd mm yy 2 2 2

Object P will have name and nested object birth_date with all the data members

Object P = 26 bytes

The compiler will not allow the definition of class birth_date because this class has already been declared in person. The compiler will not allow to declare object of birth_date as declaration of nested class is private to person. A member function of main class member has the same access control regardless whether it has been defined within its class or outside its class. Note that accessibility and visibility are independent. Visibility is based on the scoping rules of C++. A class member can be visible and inaccessible at the same time. Inline Function: - Inline functions are functions where the call is made to inline functions. The actual code then gets placed in the calling program. Advantages of Inline function Normally, a function call transfers the control from the calling program to the function and after the execution of the program returns the control back to the calling program after the function call. These concepts of function save program space and memory space and are used because the function is stored only in one place and is only executed when it is called. This execution may be time consuming since the registers and other processes must be saved before the function gets called. The extra time needed and the process of saving is valid for larger functions. If the function is short, the programmer may wish to place the code of the function in the calling program in order for it to be executed. This type of function is best handled by the inline function. In this situation, the programmer may be wondering "why not write the short code repeatedly inside the program wherever needed instead of going for inline function?". Although this could accomplish the task, the problem lies in the loss of clarity of the program. If the programmer repeats the same code many times, there will be a loss of clarity in the program. The alternative approach is to allow inline functions to achieve the same purpose, with the concept of functions. What happens when an inline function is written? The inline function takes the format as a normal function but when it is compiled it is compiled as inline code. The function is placed separately as inline function, thus adding readability to the source program. When the program is compiled, the code present in function body is replaced in the place of function call.

General Format of inline Function: The general format of inline function is as follows: inline datatype function_name(arguments) The keyword inline specified in the above example, designates the function as inline function. For example, if a programmer wishes to have a function named exforsys with return value as integer and with no arguments as inline it is written as follows:

inline int exforsys( )


1. #include <iostream> using namespace std; int exforsys(int); void main( ) { int x; cout << "n Enter the Input Value: "; cin>>x; cout << "n The Output is: " << exforsys(x); } inline int exforsys(int x1) { return 5*x1; }

2. 3. 4.
5.

6. 7.
8.

9. 10.
11.

12.
13.

14.
15.

The function call will be replaces by 5*x at the time of compilation

The function should not be inlined if: 1. Code is too large as it will take more memory 2. Function has selection( if, switch), iteration( loops) or goto statements 3. Function which is not returning value but has a return statement. 4. Function having static variables 5. Recursive Functions (function which call itself). The member function of a class if defined inside the class are inlined by default. The member function defined outside the class can be made explicitly inline by placing the word inline before their definition. In the example below, add() is an inline function. class fred { public: add(int a,int b) { return a+ b;

}}; Note: Compiler ignore the request of inline for a large function tht is being called again & again.

You might also like