Dr. Bhargavi Dept of CS CHRIST

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 7: User-Defined Functions II
Classes and Objects Presented by: Gunjan Chhabra.
CS 117 Spring 2002 Classes Hanly: Chapter 6 Freidman-Koffman: Chapter 10, intro in Chapter 3.7.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Learners Support Publications Classes and Objects.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved More about.
Constructors and Destructors
Eine By: Avinash Reddy 09/29/2016.
Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES.
Structures and Classes
Classes C++ representation of an object
2 Chapter Classes & Objects.
Class and Objects UNIT II.
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?
Abstract Data Types and Encapsulation Concepts
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
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.
Encapsulation, Data Hiding and Static Data Members
Programming with ANSI C ++
A First C++ Class – a Circle
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
11.1 The Concept of Abstraction
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?
More about OOP and ADTs Classes
This technique is Called “Divide and Conquer”.
Object-Oriented Programming Using C++
Templates.
This pointer, Dynamic memory allocation, Constructors and Destructor
Lecture 4-7 Classes and Objects
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
CS212: Object Oriented Analysis and Design
Introduction to Classes
Abstract Data Types and Encapsulation Concepts
Inheritance Dr. Bhargavi Goswami Department of Computer Science
More about OOP and ADTs Classes
Chapter 9 Objects and Classes
Constructors and destructors
Constructors and Destructors
Operator overloading Dr. Bhargavi Goswami
Classes and Objects.
Pointers Dr. Bhargavi Goswami Department of Computer Science
Submitted By : Veenu Saini Lecturer (IT)
CS410 – Software Engineering Lecture #5: C++ Basics III
Classes C++ representation of an object
Chapter 9 Introduction To Classes
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
C++ Object Oriented 1.
Creating and Using Classes
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Introduction to Classes and Objects
Presentation transcript:

Dr. Bhargavi Dept of CS CHRIST 9426669020 Classes and objects Dr. Bhargavi Dept of CS CHRIST 9426669020

Structures v/s classes Classes are extension of the idea of structure used in C What is class? It is a new way creating and implementing user defined data type. Is structure similar to class? Not completely. How? Eg. struct complex { float x; float y; }; struct complex c1,c2,c3; Can we assign values to c1,c2 and c3? Yes. Can we add two of them and assign to third? Eg. c3=c1+c2; No. Means? It is limitation of C.

Structures v/s classes One more limitation: C do not permit data hiding. How? All the structure variables can be accessed by any function for any scope. Means, structure members are public by default. Do we want it to be public always? No. Solution? Class. Class members are private by default. Class provides data hiding and user defined data types. Class represents OOPs concepts of Encapsulation, Data Hiding, Inheritance and Data Abstraction.

SPECIFYING A CLASS A CLASS is a way to bind data and its associated functions together. It allows data and functions to be hidden from external use. When defining a class, we are creating a new abstract data type which can be treated as build-in data types. Two parts: Class Declaration, describes type and scope of members. Class Function Definitions, describes how functions are implemented.

General form of class basic syntax

Visibility labels & encapsulation What are the visibility labels? Ans: Keywords private and public. Private members are accessible inside the class. Public members are also accessible outside the class. When visibility labels are missing in the class, by default everything is private. Those classes are isolated and hidden from world and serves no purpose. Binding of data and functions together into single class type variable is referred as encapsulation.

Data hiding

Class example

Accessing class members Private data can be accessed only through member functions. Main() cannot directly access private members of the class. Syn: object-name.function-name([optional- arguments]); Eg. x.getdata(10); Public data of a class can be accessed through main() function directly. But, accessing data members directly in main() defeats OOPs concept of data hiding. So, avoid accessing data members directly through main().

Defining member functions Two ways: Outside the class Inside the class Outside the class definition: Needs identity label in header. What is that? Identity label tells compiler that function belongs which class. Syntax: return-type class-name :: function-name (argument- list) { function body } Advantage: same function name is permitted in multiple class. Eg. munna, beta, babu, sweetu, guddy, cutie, etc ;)                Inside the class definition: Just replace function declaration with function definition inside the class. It is treated as inline function. inline small functions are defined inside the class. Eg. 5_1ClassImplementation.cpp

Making outside function inline Functions defined inside class becomes inline. Agree? Yes. What if we want the functions defined outside the class to become inline? Is it possible? Yes. How? Lets see->

Nesting of member functions A member function can be called inside another function of the same class. This is called nesting of member function. Eg. 5_2nesting_member_functions.cpp

Private member function Some situation needs private access to functions also. Situations: Delete bank account Increment to employee These functions must be private. How to access these functions? Using another function of the same class. Objects cannot directly access these function using dot (.) operator. Eg. 5_3PrivateMemberFunction.cpp

Classes and arrays

Lab exercise: shopping list: Output

Memory allocation for objects and class. Done How?

Static data members Characteristics: Initialized to zero when first obj is created Only one copy of member for entire class shared by all objects Visible only within the class But, Lifetime is entire program Note, type and scope of each static member variable must be defined outside the class definition. Y so? Because, static data members with class are stored separately. They are not part of an object. Only one copy is stored per class. Also called class variables.

Static data members When are they initialized? When the objects are created. Remember: Static variables are declared in class and defined outside class in the scope of source file. Also known as non-inline member functions. It can be initialized other than 0 explicitly. Use? Maintain values that are common to entire class. Eg. maintain counter of objects, records, operations, login failures, etc Eg. 5_staticMember.cpp Eg. 5_4StaticClassMember.cpp Eg. staticMemberOfClass.cpp

Static member functions Properties: Can access only static variables/functions declared in the same class. Static member function can be called using class name instead of its objects. Class-name::function-name; Means, to call static member function, you do not need an object. Static member functions have a class scope and they do not have access to the this pointer of the class. You could use a static member function to determine whether some objects of the class have been created or not. Eg. 5_5StaticMemberFunction.cpp Eg. staticMemberFunctionofClass.cpp

Array of objects Can array be of any data type? Yes. Then array can be user defined data type also such as struct and class? how? Array of objects. Definition: array of variables that are of type class are called array of objects. Access method is also same as array. Storage mechanism? Similar to multi-dimensional array. Require memory for only class variables and not for class functions. See next fig. 5_6_array_of_objects.cpp

Array of objects

Object as functional requirement Can we use obj as function argument? Yes. How? Two ways. 1. Copy the obj and pass it to function (pass by value) 2. Only pass address of obj to function (pass by reference) Which method is Efficient? Pass by reference. See next fig. Eg. 5_7obj_as_argument.cpp Obj can also be passed as an argument to non-member function. But, such function can be passed to only public functions using objects. Also such functions cannot access private data. We will see example later in this chapter.

Object as functional requirement

Friendly functions If we need common function of multiple class what to do? Can two classes share same function? Eg. academics and nonacademic are two classes which needs a common function called income_tax() Friend function can access private data of other class. Friend function need not be member of any of these two classes. How to do that?

Friendly functions

Friendly functions Characteristics Is not defined in the scope of class where it is declared. So, it cannot be called using obj of that class. Like a normal function, it is invoked without help of any object. It cannot access class data variables directly similar to class functions. Can be declared in any scope, private or public. Usually has objects as arguments. Used for operator overloading.

Function of one class is friend function of another class

All function of one class can be friend function of another class

Friend function Eg. 5_8_friend_function.cpp Eg. 5_9_function_friendly_to_two_classes.cpp Eg. 5_10_swapping-private-data-of-classes.cpp Eg. friendFunction.cpp Eg. friendClass.cpp Eg. friendFunctionInAnotherClass.cpp Eg. friendVar.cpp Eg. globalFriendFunction.cpp

Returning objects Can we return objects same as arguments? Can we create objects within a function and return to another function? Yes. Lets see how. Eg. 5_11_returning_objects.cpp

Const member function If member function does not alter any data in the class, then we may declare it as a const member function. Qualifier ‘const’ is applied to both, declaration and definition. Compiler will give error if such functions carry assignment statements. Eg. void mul(int,int) const; double balance() const;

END OF CHAPTER 5 THANK YOU