Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.

Slides:



Advertisements
Similar presentations
1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Advertisements

Chapter 7: User-Defined Functions II
Chapter 14: Overloading and Templates
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Review of C++ Programming Part II Sheng-Fang Huang.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Learners Support Publications Classes and Objects.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Object Oriented Programming (OOP) Lecture No. 8. Review ► Class  Concept  Definition ► Data members ► Member Functions ► Access specifier.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
CONSTRUCTOR AND DESTRUCTORS
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Constructors and Destructors
Eine By: Avinash Reddy 09/29/2016.
Learning Objectives Pointers as dada members
2 Chapter Classes & Objects.
Chapter 6 CS 3370 – C++ Functions.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Static data members Constructors and Destructors
Introduction to C++ Systems Programming.
CONSTRUCTORS & DESTRUCTORS
Java Primer 1: Types, Classes and Operators
Classes & Objects.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 5 Classes.
Constructor & Destructor
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Class: Special Topics Copy Constructors Static members Friends this
Chapter 15: Overloading and Templates
This pointer, Dynamic memory allocation, Constructors and Destructor
C++ for Engineers and Scientists Second Edition
7 Arrays.
CS212: Object Oriented Analysis and Design
Chapter 9 Classes: A Deeper Look, Part 1
Dr. Bhargavi Dept of CS CHRIST
Constructors and destructors
Constructors and Destructors
Classes and Objects.
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
COP 3330 Object-oriented Programming in C++
Submitted By : Veenu Saini Lecturer (IT)
A Deeper Look at Classes
C++ Class Members Class Definition class Name { public: constructor(s)
C Programming Lecture-17 Storage Classes
Constructors & Destructors
More C++ Classes Systems Programming.
Storage classes in C In C language, each variable has a storage class which decides the following things: scope i.e where the value of the variable would.
INTRODUCTION TO C.
SPL – PS3 C++ Classes.
Presentation transcript:

Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other class. For example a Linked List class may be allowed to access private members of Node.

Static Data Members and Member Functions

Static Data Member We can define class members static using static keyword. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member.

A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present. We can't put it in the class definition but it can be initialized outside the class by re declaring the static variable, using the scope resolution operator :: to identify which class it belongs to. Data type Class Name : : Static Variable

Member Function Types of Member Functions Simple functions Static functions Const functions Inline functions Friend functions

Simple Member functions return_type functionName(parameter_list) { function body; }

Static Member functions A function is made static by using static keyword with function name. These functions work for the class as whole rather than for a particular object of a class. It can be called using the object and the direct member access . operator. But, its more typical to call a static member function by itself, using class name and scope resolution :: operator.

class X { public: static void f() } }; int main() X::f(); // calling member function directly with class name return 0;

Constant Member Functions Declaring a member function with the const keyword specifies that the function is a "read-only" function that does not modify the object for which it is called. Const class object has been initialized via constructor, any attempt to modify the member variables of the object is disallowed, as it would violate the const-ness of the object. Const class object cannot call the normal member functions. It can call only constant member functions. It is recommended practice to make as many functions const as possible so that accidental changes to objects are avoided.

To declare a constant member function, place the const keyword after the closing parenthesis of the argument list. The const keyword is required in both the declaration and the definition. //Basic Syntax of const Member Function return_type function_name() const { };

Inline functions Inline function is a function that is expanded in line when it is called. When the inline function is called whole code of the inline function gets inserted or substituted at the point of inline function call. This substitution is performed by the C++ compiler at compile time. Inline function may increase efficiency if it is small. Syntax: inline return type fun_name(arguments) { }

Limitations of Inline Function Inline function sends request not command to the compiler if function too long means it consider as the normal functions. Compiler can ignore the request for in lining. Compiler may not perform in lining in such circumstances like: 1)If a function contains a loop. (for, while, do-while) 2) If a function contains static variables. 3) If a function is recursive. 4) If a function return type is other than void, and the return statement doesn’t exist in function body. 5) If a function contains switch or goto statement.

Friend functions Friend functions are actually not class member function. Friend functions are made to give private access to non-class functions

Constructors A class constructor is a special member function of a class that is executed whenever we create new objects of that class. A constructor will have exact same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for setting initial values for certain member variables.

Declaring Constructor: constructor_name(*similar to classname*); Defining Constructor: Inline constructor Construtor name(int,int); integer(int x,int y){m=x; n=y;} Outside Definition Classname::constructor name(int x, int y) { m=x; n=y; } Calling constructor: Example e = Example(0, 50); // Explicit call Example e(0, 50); // Implicit call

Types of Constructors Constructors are of three types : Default Constructor Parametrized Constructor Copy Constructor Destructor

Default Constructor Default constructor is the constructor which doesn't take any argument. It has no parameter. Syntax : class_name () { Constructor Definition }

Parameterized Constructor These are the constructors with parameter. Using this Constructor you can provide different values to data members of different objects, by passing the appropriate values as argument.

Copy constructor The copy constructor is a constructor which creates an object by initializing it with an object of the same class Syntax ClassName (const ClassName &objname) { };

Destructors A destructor is a special member function that is called when the lifetime of an object ends. The purpose of the destructor is to free the resources that the object may have acquired during its lifetime.

Designate a function as a class's destructor by preceding the class name with a tilde (~). For example, the destructor for class String is declared: ~String().

Pointers A pointer is a variable whose value is the address of another variable. Syntax: type *var-name;

C++ Null Pointers C++ supports null pointer, which is a constant with a value of zero defined in several standard libraries. C++ pointer arithmetic There are four arithmetic operators that can be used on pointers: ++, --, + C++ array of pointers You can define arrays to hold a number of pointers. C++ pointer to pointer C++ allows you to have pointer on a pointer and so on. Passing pointers to functions Call by reference

C++ NULL pointers It is always a good practice to assign the pointer NULL to a pointer variable in case you do not have exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer. int *ptr = NULL;

Pointer to Pointer A pointer to a pointer is a form of multiple indirection or a chain of pointers. Normally, a pointer contains the address of a variable. When we define a pointer to a pointer, the first pointer contains the address of the second pointer, which points to the location that contains the actual value

This Pointer Every object in C++ has access to its own address through an important pointer called this pointer. The this pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object. Friend functions do not have a this pointer, because friends are not members of a class. Only member functions have a this pointer.

References A reference variable is an alias, that is, another name for an already existing variable. Once a reference is initialized with a variable, either the variable name or the reference name may be used to refer to the variable. Syntax: int & r = i

Storage Class A storage class defines the scope (visibility) and life-time of variables and/or functions within a C++ Program. These specifiers precede the type that they modify. There are following storage classes, which can be used in a C++ Program auto register static extern mutable

Storage Class Keyword Lifetime Visibility Initial Value Automatic auto Function Block Local Garbage External extern Whole Program Global Zero Static static Register register Mutable mutable Class

The auto Storage Class The auto storage class is the default storage class for all local variables. auto can only be used within functions, i.e., local variables.

The register Storage Class The register storage class is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) The register should only be used for variables that require quick access such as counters. Syntax: register int miles;

The static Storage Class The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope

Extern Storage Class The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern' the variable cannot be initialized as all it does is point the variable name at a storage location that has been previously defined.

First File: main.cpp #include <iostream> int count ; extern void write_extern(); main() { count = 5; write_extern(); } Second File: support.cpp #include <iostream> extern int count; void write_extern(void) { std::cout << "Count is " << count << std::endl; }

$g++ main.cpp support.cpp -o write 5

Mutable storage class mutable storage class is applicable to only class data members. constant variable cannot be modified in the program. This is applicable to objects also. If a data member of a class is declared as mutable, then it can be modified by an object or functions which is declared as constant.