Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Similar documents
CLASSES AND OBJECT CHAPTER 04 CLASS XII

OBJECTS. An object is an entity around us, perceivable through our senses. Types of Object: Objects that operate independently.

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Data Structures using OOP C++ Lecture 3

CS304 Object Oriented Programming Final Term

ECE 122. Engineering Problem Solving with Java

Introduction to Classes

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat Chapter 6 Inheritance Extending a Class

C++ 8. Constructors and Destructors


Chapter 6 Introduction to Defining Classes

Chapter 4 Defining Classes I

Interview Questions of C++

Abstraction in Software Development

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Classes. Logical method to organise data and functions in a same structure. Also known as abstract data type (ADT).

Darshan Institute of Engineering & Technology for Diploma Studies


Anatomy of a Class Encapsulation Anatomy of a Method

Recharge (int, int, int); //constructor declared void disply();

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

C++ Important Questions with Answers

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Object Oriented Programming(OOP).

Kapi ap l S e S hgal P T C p t u er. r S. c S ienc n e A n A k n leshw h ar ar Guj u arat C C h - 8

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING CS6456 OBJECT ORIENTED PROGRAMMING

IV Unit Second Part STRUCTURES

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Implementing Abstract Data Types (ADT) using Classes

STRUCTURING OF PROGRAM

Topics. Topics (Continued) 7.1 Abstract Data Types. Abstraction and Data Types. 7.2 Object-Oriented Programming

Chapter 13: Introduction to Classes Procedural and Object-Oriented Programming

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++


Abstract Data Types (ADT) and C++ Classes

Lecture #1. Introduction to Classes and Objects

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 12: Classes and Data Abstraction

Lecture 7. Log into Linux New documents posted to course webpage

CHAPTER 4 FUNCTIONS. 4.1 Introduction

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

CS304 Object Oriented Programming

Object Oriented Programming. Solved MCQs - Part 2

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

CS201 Some Important Definitions

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Ticket Machine Project(s)

UNDERSTANDING CLASS DEFINITIONS CITS1001

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

public class TicketMachine Inner part omitted. public class ClassName Fields Constructors Methods

Chapter 4: Writing Classes

Classes. C++ Object Oriented Programming Pei-yih Ting NTOU CS

Introduction Of Classes ( OOPS )

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

EECS168 Exam 3 Review

public : int min, hour ; T( ) //here constructor is defined inside the class definition, as line function. { sec = min = hour = 0 ; }

Absolute C++ Walter Savitch

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. 1

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

INHERITANCE: EXTENDING CLASSES

Object-Oriented Programming (OOP) Fundamental Principles of OOP

Chapter 17: Nested Classes

AC55/AT55 OBJECT ORIENTED PROGRAMMING WITH C++ DEC 2013

l A class in C++ is similar to a structure. - It allows you to define a new (composite) data type. l A class contains the following: - variables AND

G Programming Languages - Fall 2012

Problem Solving with C++

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Inheritance, and Polymorphism.

Example : class Student { int rollno; float marks; public: student( ) //Constructor { rollno=0; marks=0.0; } //other public members };

Abstract Data Types. Different Views of Data:

+2 Volume II OBJECT TECHNOLOGY OBJECTIVE QUESTIONS R.Sreenivasan SanThome HSS, Chennai-4. Chapter -1

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

CHAPTER 9 INHERITANCE. 9.1 Introduction

Lecture-5. Miscellaneous topics Templates. W3101: Programming Languages C++ Ramana Isukapalli

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING OBJECT ORIENTED PROGRAMMING CLASS : THIRD SEMESTER CSE

Introduction to Programming Using Java (98-388)

C++ : Object Oriented Features. What makes C++ Object Oriented

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

C++ Addendum: Inheritance of Special Member Functions. Constructors Destructor Construction and Destruction Order Assignment Operator

Encapsulation. Mason Vail Boise State University Computer Science

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

I BSc(IT) [ Batch] Semester II Core: Object Oriented Programming With C plus plus - 212A Multiple Choice Questions.

! Data is stored in variables. - Perhaps using arrays and structs. ! Program is a collection of functions that perform

Preview 9/20/2017. Object Oriented Programing with C++ Object Oriented Programing with C++ Object Oriented Programing with C++

Chapter 10 Introduction to Classes

Object Oriented Software Design II

Classes. Christian Schumacher, Info1 D-MAVT 2013

3. Basic Concepts. 3.1 Application Startup

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Next week s homework. Classes: Member functions. Member functions: Methods. Objects : Reminder. Objects : Reminder 3/6/2017

Encapsulation in C++

Implementing Subprograms

Lecture 18 Tao Wang 1

Object Oriented Software Design II

Transcription:

Classes Chapter 4 Classes and Objects Data Hiding and Encapsulation Function in a Class Using Objects Static Class members

Classes Class represents a group of Similar objects A class is a way to bind the data describing an entity and its associated function together. Consider an account having characteristics Account no., type, balance Its associated operations are DEPOSITE and WITHDRAWAL.

Class Definition (Construct) class account { int acno; int type; float balance; float deposit( float amt) { balance = balance + amt; } float withdrawal (float amt) { balance = balance amt; } };

Why class are needed? In the software field of Information Technology Industry, a need was felt to create software to real world object. Real world objects certainly have data- type properties such as account no. balance etc. But real world object also can also do certain things like deposit and withdrawal. Now to combine all these properties of real world objects, one needed a way out. The structure of C could provide a half way help by binding the data type properties but still the operation association left out.

Declaration of Class The declaration of class involves declaration of its four associated attributes: Data members are the data type properties that describe the characteristics of a class. There may be zero or more data members in a class Member function are the set of operations that may be applied to objects of that class. There may be zero or more member function for a class. They are referred as class interface.

Continue Program Access Level that control access to member from within the program. These access levels are PRIVATE, PROTECTED and PUBLIC. Depending upon the access level of a class member access to it is allowed or denied. Class tagname that serves as a type specifier for the class using which objects of this class type can be created.

Structure of Class class classname { private: private members and function visible only inside the class protected:. protected members and function visible class and his child classes public:.. Visible in any where in the program }

Class Method Definition Methods (Member Function) can be defined from two ways Inside the class Outside the class.

Inside the class - Method Definition Methods of class often define in public section. class name { private:.. Data member; public: return-type type method_name (paramete list) {.. Function Body } };

Outside the class - Method Definition return-type type class_name :: method_name (Parameter list) {.. Function body } Here :: pronounced as Scope Resolution Operator

Difference between struct and class In struct all members are public but in class all members by default be private. In struct there is no methods but class hold members as well as methods. struct can not derived any other struct, but class can derive any other class.

Array within the class A class can have an array as its member variable. An array in the class can be private or public data member of the class. class exarray { int arr[10]; public: int largest(); int sum(); };

Scope of Class and its member Public members are the members that can access directly by any function. Whether member function of the class or non member function Private Member are the class members that are hidden from the outside world. The private member of the class can be used by the member function of the class

Scope Rules and Class Global Class A class is said to be global class if its definition occurs outside the bodies of all functions in a program. Which means that object of this class type can be declared from any where in the program. Local Class A class is said to be local if its definition occurs inside a function body. Which means that object of this class can be declared only within the function.

Example #include<iostream.h> void main() { // main started class y { // class started }; // class end } // main end

Scope for Objects Global Object An object is said to be global Object if it is declared outside all the function bodies and it means that this object is globally available to all function in the program. This object can be used any where in the program.

Local Object An object is said to be local if it is declared within a function, which means that this object is locally available to the function that declares it and it cannot be used outside the function declaring it.

Example #include<iostream.h> class X {.. }; X obj1,obj2; // void main() { X obj3,obj4; } // Global object; //Local object

Scope of Private and Protected member The PRIVATE and PROTECTED members of a class have class scope. It means these can be accessed only by the member function of the same class. These members can not be accessed directly by using the object names. In other words nowhere except the bodies of member function can be names of private and protected member appear.

Scope of Public members The scope of the public members depends upon the object being used for referencing them. If the referencing object is a global object, then the scope public members is local.

#include<iosream.h void main() void fun1() { class x { private : int a; { x obj2; x obj3; void fc() { cout<<a; } public: int i; void fcc() { cout<<2*i; A=13; obj1.i=10; obj1.fcc(); obj2.i=20; obj2.fcc(); } obj1.i=12; obj1.fcc(); obj2.i=25 obj2.fcc(); obj3.i=30; obj3.fcc(); Valid Valid Invalid Invalid Valid Valid fc();}}; } x obj1;

Types of Class Function Accessor Function Mutator Function Manager Function

Accessor Function These Function are those which can not change the data members value, but they simply use for the retrieving the value of data members; It means, Accessor function are used to read values of private data members of a class which are directly not accessible in non member function.

class student { int rollno; float marks; char name[25]; char grade; public: void readdata() { } void printdata() { } int getrollno() { return(rollno); } float getmarks() { return(marks); } };

Mutator Function This type of function are used to manipulate the value of private data members. Generally most of the member function are such type.

class student { int rollno; float marks; char name[25]; char grade; public: void readdata() { } void printdata() { } void calculate() { if marks>=90 grade='a'; else if marks>=75 grade='b'; else if marks>=60 grade='c'; else grade='d'; }

Manager Function These are the member function with specific type. This types of function are also CONSTRUCTOR and DESTRUCTOR. These function are used to initialize and destroying the objects.

Nested Class A class is declared within another class. A class is declared within another class is called nested class. The outer class is class is called enclosing class and inner class is called the nested class.

Member Access Control of Nested Class The member function of nested classes have no special access to members of an enclosing class. They obey the usual access rule and the same is true for enclosing class for the access of nested class members Nested class can access public member of enclosing by own object and similarly enclosing class can access inner class public member by own object. Private and Protected member is not available for other class.

Inline Function The inline function are a C++ enhancement designed to speed up programs. The coding of normal function and inline function is similar except that inline functions definition starts with the keyword inline. The distinction between normal function and inline function is the different compilation process for them.

Original Program void main {...... square(3);...... square(3);...... square(3); } void square(int i) { cout<<i*i; } Compiled Program void main {...... { cout<<3*3; }...... { cout<<3*3; }...... { cout<<3*3; } }

When we make function inline The function should be inline only when they are small The function which does not contains return value, loop, switch and goto statement. The function which does not contains static variables. The function should not recursive.

Constant Member Function If a member function of a class does not alter any data in the class. This member function may be declared as constant member function using the keyword const Example. int max (int, int) const; void prn(void) const;

Use of :: Operator int x,y; class A { public: int x; void f(int x) { x=i; ::x=i; y=i; } };

Array of Object C++ Supports arrays of any data type including class type. An array having class type elements is known as Array of Object. Array of Object is declared after the class definition is over and it is defined in the same way as other array is defined.

Object as Function Arguments Just like any other data types of C++, an object is also passed as function argument. An Object can also be passed as Call by Value and Call by Reference An Object can be passed to any member function, non member function and any friend function.

Function Returning Object An object can not only be passed to the function but a function can also return an object. In other words, A function can also be a class type. student total (student); distance add (distance d, distance e) { distance k; --- --- Return(k); }

Static Data Members Static data members of a class just like a global variable for its class. The static data members are stores value common to the entire class.

Difference between static and simple data members There is only one copy of the data members of a class is maintained for the entire class, which is shared by all the object of the class. It is visible only within the class. However its life time (the time for which it remain in memory) is the entire class.

How to make static data member Declaration within the class class X { static int count; }; Definition outside the class Type ClassName :: DataMember = value; int X :: count = 0; A static data member must be defined outside the the class definition.

Initialization of const Static data Members const data members of standard data type now can be initialized inside the class. Initialization is also a type of definition, so no further definition is required outside the class. class demo { static const int amount =400; static const float val =50; };

Static Member Function A static member function that accesses only the static data members of a class may be declared as static. This can be done by putting the keyword static before the function declaration in the class definition. class demo { static int a; public: static void show() {... } };

Difference between static member function and other member function A static member function can access only static data members of the same class, But other member function can access both type of data member.. A static member function can invoked by using the class name instead of object of class like classname :: staticfunctionname()