1 CSC241: Object Oriented Programming Lecture No 02.

Slides:



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

Objects and Classes Part II
1 CSC241: Object Oriented Programming Lecture No 21.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
CSC241: Object Oriented Programming
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CSC241: Object Oriented Programming Lecture No 13.
You gotta be cool. Introduction to Classes, Objects and Strings Introduction Defining a Class with a Member Function Defining a Member Function with a.
Learners Support Publications Classes and Objects.
Chapter 10 Introduction to Classes
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
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.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
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.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
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?
1 CSC241: Object Oriented Programming Lecture No 05.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 CSC241: Object Oriented Programming Lecture No 03.
Programming Techniques Classes II Important Class Features Spring 2009.
1 Introduction to Object Oriented Programming Chapter 10.
1 CSC241: Object Oriented Programming Lecture No 08.
Programming Fundamentals1 Chapter 7 INTRODUCTION TO CLASSES.
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.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Learners Support Publications Constructors and Destructors.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
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.
Constructors and Destructors
Visit for more Learning Resources
3 Introduction to Classes and Objects.
CSC241: Object Oriented Programming
Chapter 3: Using Methods, Classes, and Objects
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Introduction to Classes and Objects
Constructors and Destructors
Classes and Objects.
Object Oriented Programming Using C++
Object Oriented Programming in java
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Constructors and Deconstructor
Constructors & Destructors
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

1 CSC241: Object Oriented Programming Lecture No 02

2 Previous lecture A simple analogy – A class is a blue print e.g. car engineering drawing – An object has existence e.g. my car Each object has its attributes (variables) and behavior (functions) To perform a task describe by class, an object of that class must be created Class definition begins with keyword class Class name begins with capital letter Class definition is enlosed in pair of braces { }

3 Cont. Member function that appear after the access specifier public can be called by other function and member functions of that class Access specifier are always followed by : Each message sent to an object is a member- function call that tells the object to perform a task Member function can be access by object of class using dot operator

4 Defining a class with member function

5 Today’s Lecture Simple C++ program using cin and cout Member function with parameters Set functions and get functions Constructor

6 Member function with parameter Write a program

7 A book class

8 Class data The class Book contain three data items – char name[15]; – int pages; – float price; There can be any number of data members in a class just as in structure Data member are after the keyword private, so they can be accessed from within the class, but not outside

9 Member function These functions are included in a class There are four member functions in class Book – setName(char *n) – setPages(int p) – setPrice(float p) – display() These functions are followed by a keyword public, so they can be accessed outside the class

10 Class data and member function Access specifier label public and private Function are public and data is private Data is hidden so that it can be safe from accidental manipulation Functions operates on data are public so they can be accessed from outside the class

11 Defining Objects name pages price b1 Operating system

12 Cont. Defining an object is similar to defining a variable of any data type: Space is set aside for it in memory e.g. int x; Defining objects in this way (book b1;) means creating them An object is an instance (that is, a specific example) of a class. Objects are sometimes called instance variables.

13 Calling Member Functions The next four statements in main() call the member function – b1.setName("Operating System"); – b1.setPages(500); – b1.setPrice(150.56); – b1.display(); don’t look like normal function calls This syntax is used to call a member function that is associated with a specific object It doesn’t make sense to say – changeName("Operating System"); because a member function is always called to act on a specific object, not on the class in general

14 Cont. To use a member function, the dot operator (the period) connects the object name and the member function. The syntax is similar to the way we refer to structure members, but the parentheses signal that we’re executing a member function rather than referring to a data item. The dot operator is also called the class member access operator. Go to program

15 Data members, set and get functions

16 Example program – Distance class Data members – Feet – Inches Member functions – void setdist(int ft, float in); – void getdist(); – void initialize(); – void showdist(); Go to program

17 Distance class – data member initialization Distance class shows two ways to initialize the data items in an object – void initialize(); – void setdist(int ft, float in); Can an object be initialized whenever it is created, without requiring a separate call to a member function? Automatic initialization is carried out using a special member function called a constructor. A constructor is a member function that is executed automatically whenever an object is created.

18 Constructors C++ requires a construct call for each object it has created This ensure that object is initialized properly before it is used If there is no constructor, the compiler provides a default constructor that is, a constructor with no parameters Name of constructor function is same as name of class

19 A counter example Data member – Count Member function – Constructor – void inc_count() – int get_count() Go to program

20 Automatic initialization An object of type Counter is first created, we want its count to be initialized to 0 Options are – set_count() function (call it with an argument of 0) – zero_count() function, to set count to 0. Such functions would need to be executed every time we created a Counter object

21 Cont. A programmer may forget to initialize the object after creating it It’s more reliable and convenient to cause each object to initialize implicitly when it is created In the Counter class, the constructor Counter() is called automatically whenever a new object of type Counter is created Counter c1, c2; creates two objects. Constructor is called with each object separately Go to program

22 Constructor Name First, constructor name must be same as the name of class – This is one way the compiler knows they are constructors Second, no return type is used for constructors – Why not? Since the constructor is called automatically by the system, there’s no program for it to return anything to; a return value wouldn’t make sense – This is the second way the compiler knows they are constructors

23 Initializer List One of the most common tasks a constructor carries out is initializing data members In the Counter class the constructor must initialize the count member to 0 The initialization takes place following the member function declarator but before the function body. Initialization in constructor’s function body Counter() { count = 0; } this is not the preferred approach

24 Cont. It’s preceded by a colon. The value is placed in parentheses following the member data. Counter() : count(0) { } If multiple members must be initialized, they’re separated by commas. – someClass() : m1(7), m2(33), m2(4) ←initializer list { }

25 Placing a Class in a Separate File Header file (.h) – Contain definition of class – Not an executable file – Included in.cpp file main () file (.cpp) – Include.h file containing class definition – Executable file Go to program

26 Destructors Destructor is a function called automatically when an object is destroyed The most common use of destructors is to deallocate memory that was allocated for the object by the constructor Go to program

27 Validating Data with set Functions A set function should validate the value before assigning to private data member Set function can return a value or may display a message if invalid data is assign to object Go to program