Class and Objects UNIT II.

Slides:



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

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
C++ fundamentals.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Learners Support Publications Classes and Objects.
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.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
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.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
Chapter 2 Objects and Classes
Constructors and Destructors
Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES.
DATA HIDING AND MEMBER FUNCTIONS
Friend functions.
Classes (Part 1) Lecture 3
2 Chapter Classes & Objects.
By Muhammad Waris Zargar
Examples of Classes & Objects
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.
Review: Two Programming Paradigms
This technique is Called “Divide and Conquer”.
Prepared By: G.UshaRani B.Pranalini A.S.Lalitha
group work #hifiTeam
Lecture 4-7 Classes and Objects
Constructor & Destructor
Contents Introduction to Constructor Characteristics of Constructor
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Return_DT F_name ( list of formal parameters)
Classes & Objects: Examples
Learning Objectives Classes Constructors Principles of OOP
Dr. Bhargavi Dept of CS CHRIST
Constructors and destructors
Constructors and Destructors
Classes and Objects.
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
The Function Prototype
9-10 Classes: A Deeper Look.
Submitted By : Veenu Saini Lecturer (IT)
Types of Computer Languages
OOP objects and Classes Dr. Ahmed Hashim Mohammed A Simple Class
9-10 Classes: A Deeper Look.
Object Oriented Programming (OOP) Lecture No. 12
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Functions Chapter No. 5.
Introduction to Classes and Objects
Presentation transcript:

Class and Objects UNIT II

UNIT II BASIC CHARACTERISTICS OF OOP Data Hiding and Member Functions Object Creation and Destruction Polymorphism data abstraction Iterators and Containers

Today’s class is about classes and objects

An Introduction to classes A class is a building block of OOP. It is the way to bind the data and its logically related functions together. An abstract data type that can be treated like any other built in data type.

Class definition Class head class name_of_class. Class body { data members; member functions; };

Example Class test { private : int a; int b; public: void set_data(int x, int y) a=x; b=y; } int big() if (a > b) return a; else return b; };

C++ Gradebook Example Declaring an object of this class as an automatic variable of main I.e., allocated on The Stack

Member Functions with Parameters Include string class definition Member function parameter Use the function parameter as a variable

Member Functions with Parameters (cont.) Passing an argument to the member function

Characteristics of access specifiers (private,public and protected) Private section of a class can have both data members and member functions, usually data members are made private for data security. It is not mandatory that private section has to declared first in the class and then the public section. If no member access specifier is specified then by default the members are private for the class. There may be any number of private, public or protected section in a class declaration. Protected specifier is used for declaring the class members which can be accessed by its own class and its derived class.

Member Function Member function’s name is visible outside the class. It can be defined inside or outside the class. It can have access to private, public and protected data members of its class, but cannot access private data members of another class.

Introduction to objects Object is an abstraction of real wold entity. Objects are the variables/instances of classes. Syntax for declaring objects is as follows : <class name> <obj_name1>,<obj_name2>, …, <obj_name1>; Example: for class test the objects can be created as follows: test t1,t2,…tn;

Characteristics of objects: It can have its own copy of data members. The scope of an object is determined by the place in which the object is defined. It can be passed to a function like normal variables. The members of the class can accessed by the object using the object to member access operator or dot operator(.).

Example: Class test cout<<“enter the two numbers” << endl; { cin>> a >> b; private : t.set_data(a,b); int a; cout<<“the largest number is ” << t.big() << endl; int b; } public: void set_data(int x, int y) { a=x; b=y; } int big() if (a > b) return a; else return b; }; void main() test t; int a,b;

Definition of function outside a class. The syntax for defining function outside a class is as follows: <data type> <class name> :: <function name> () { // member function definition } Data type return type Class name the class to which the member function belongs. Function name name of member function.

Example: Class test { private : int a; int b; public: void set_data(int , int ); int big(); }; void test :: set_data(int x, int y) a=x; b=y; } int test :: big() if (a > b) return a; else return b;

The Arrow operator It is also called as pointer to member access operator. It is used when member functions or member data has to access through a pointer which is pointing to an object of a class. Syntax for using arrow operator is : Pointer_to_object -> class_member;

Example of arrow operator Class test1 { private : int a; int b; public: void add(int , int ); }; void test1 :: add(int x, int y) a=x; b=y; return (a+b); } void main() { int sum; test1 t; sum = t.add(4,9) cout<< sum << endl; test1 *t1 = &t; sum = t1 -> add(2,7); cout<< sum<<endl; } Output: 13 9

This operator It is a keyword used to store the address of the object that invokes a member function. When each member function is invoked this pointer implicitly holds the address of the object itself. It is defined internally. When an object is used to invoked a class member function then the address of that object is automatically assigned to the this pointer.

Example showing the explicit use of this pointer : #include<iostream> class simple { int a; public: void set_data( int x) this -> a = x; } void display() cout<<this -> a<<endl; cout<<“address of the object is =”<< this<<endl; }; void main() { simple s; s.set_data(7); s.display(); } Output: 7 address of the object is = 0X8feeff4