Introduction to Classes and Objects CS-2303, C-Term 20101 Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.

Slides:



Advertisements
Similar presentations
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
Advertisements

 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 Introduction.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
A Deeper Look at Classes CS-2303, C-Term A Deeper Look at Classes CS-2303 System Programming Concepts (Slides include materials from The C Programming.
 2006 Pearson Education, Inc. All rights reserved Introduction.
 2008 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.
Chapter 11: Classes and Data Abstraction
CS-2303 System Programming Concepts
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 01] Introduction to.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Introduction to C++ Part II.
You gotta be cool. Introduction to Classes, Objects and Strings Introduction Defining a Class with a Member Function Defining a Member Function with a.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
1 Introduction to Classes and Objects Chapter 3 Introduction to Classes and Objects Chapter 3.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 4 Introduction to Classes, Objects, Methods and strings
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.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
計算機程式語言 Lecture 03-1 國立台灣大學生物機電系 林達德 3 3 Introduction to Classes and Objects.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 12: Classes and Data Abstraction.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 3 Part II. 3.8 Placing a Class in a Separate File for Reusability.cpp file is known as a source-code file. Header files ◦ Separate files in which.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
CLASSES AND OBJECTS Chapter 3 : constructor, Separate files, validating data.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved. Note: C How to Program, Chapter 16 is a copy of C++ How to Program Chapter.
Chapter 3 Introduction to Classes, Objects and Strings C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor.
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 Classes and Objects
Chapter 3 Introduction to Classes, Objects and Strings
Chapter 3 Introduction to Classes and Objects
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
Chapter 9 Classes: A Deeper Look, Part 1
10.2 const (Constant) Objects and const Member Functions
3-4-5 Introduction.
Introduction to Classes and Objects
Classes, Constructors, etc., in C++
Object Oriented Programming in java
A Deeper Look at Classes
Introduction to Classes and Objects
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
CS148 Introduction to Programming II
More C++ Classes Systems Programming.
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

Introduction to Classes and Objects CS-2303, C-Term Reading Deitel & Deitel, Chapter 19

Introduction to Classes and Objects CS-2303, C-Term Classes and Objects Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors Placing Classes in Separate Files Separating Interface from Implementation Data Validation –Ensures that data in an object is in a particular format or range.

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 Each containing data members and member functions.

Introduction to Classes and Objects CS-2303, C-Term Defining a Class With a Member Function Class definition –Tells the compiler what member functions and data members belong to the class. –Keyword class followed by the class’s name. {} –Class body is enclosed in braces ( {} ) Specifies data members and member functions :Access-specifier public: –Indicates that a member function or data member is accessible to other functions and member functions of other classes.

Introduction to Classes and Objects CS-2303, C-Term C++ Gradebook Example Deitel & Deitel Fig 19.1 “using”:– add the name to the current scope

Introduction to Classes and Objects CS-2303, C-Term Systems Programming: Classes and Objects 7 C++ Gradebook Example Beginning of class definition for class GradeBook Beginning of class body End of class body Access specifier public ; makes members available to the public Member function displayMessage returns nothing Use dot operator to call GradeBook ’s member function

Introduction to Classes and Objects CS-2303, C-Term C++ Gradebook Example Declaring an object of this class as an automatic variable of main I.e., allocated on The Stack

Introduction to Classes and Objects CS-2303, C-Term Member Functions with Parameters Include string class definition Member function parameter Use the function parameter as a variable

Introduction to Classes and Objects CS-2303, C-Term Member Functions with Parameters (cont.) Passing an argument to the member function

Introduction to Classes and Objects CS-2303, C-Term Useful Tidbits A string –Represents a string of characters. –An object of C++ Standard Library class std::string Defined in header file. –Not a character array as in C. Library function getline –Used to retrieve input until newline is encountered –Example getline( cin, nameOfCourse ); –Inputs a line from standard input into string object nameOfCourse. Defined in header file.

Introduction to Classes and Objects CS-2303, C-Term Data Members of a Class Declared in the body of the class May be public or private Exist throughout the life of the object. Stored in class object. Each object has its own copy. May be objects of any type

Introduction to Classes and Objects CS-2303, C-Term Access-specifier private Makes any member accessible only to member functions of the class. May be applied to data members and member functions Default access for class members Encourages “information hiding”

Introduction to Classes and Objects CS-2303, C-Term set function modifies private data get function accesses private data Public and Private Members

Introduction to Classes and Objects CS-2303, C-Term Use set and get functions, even within the class Accessing private data outside class definition private members accessible only to member functions of the class default constructor Public and Private Members (continued)

Introduction to Classes and Objects CS-2303, C-Term Modifying private data outside class definition default setting from constructor is an empty string!! Public and Private Members (continued)

Introduction to Classes and Objects CS-2303, C-Term Software Engineering Observation 19.1 As a rule of thumb, data members should be declared private Member functions should be declared public. –Except member functions that are accessed only by other member functions of the class. Often useful to have get and set functions –To access private members in controlled ways

Introduction to Classes and Objects CS-2303, C-Term Constructors and Destructors Constructor:– a function used to initialize the data of an object of a class –Same name as class itself –Cannot return anything, not even void –A class may define more than one constructor With different parameter lists Default constructor has no parameters Called automatically –When class object is declared as automatic variable –By new operator Compiler provides one if you do not! Compiler’s default simply calls constructors of data members of the class.

Introduction to Classes and Objects CS-2303, C-Term Constructors and Destructors (continued) Destructor:– a function used to clean up an object of a class prior to deleting that object –Class name preceeded by '~' –No parameters, no result Called automatically –When function exits scope of automatic class object –By delete operator Compiler provides one if you do not! Compiler’s default simply calls destructors of data members of the class.

Introduction to Classes and Objects CS-2303, C-Term Constructors and Destructors (continued) Constructors – Similar to Java Destructors – No counterpart in Java Purpose of Destructors Free dynamic storage pointed to only by members of object Reduce reference count when object disappears Safely close things – e.g., files …

Introduction to Classes and Objects CS-2303, C-Term Constructor Example Constructor has same name as class and no return type Initialize data member

Introduction to Classes and Objects CS-2303, C-Term Constructor Example Destructor need so this class object can free dynamically allocated memory

Introduction to Classes and Objects CS-2303, C-Term Constructor Example Creating objects implicitly calls the constructor

Introduction to Classes and Objects CS-2303, C-Term Class in a Separate Header File for Reusability.cpp files for source-code implemenations –Class implementations –Main programs –Test programs –… Header files –Separate files in which class definitions are placed. –Allow compiler to recognize the classes when used elsewhere. –Generally have.h filename extensions Driver file –A program used to test software (such as classes). –Contains a main function so it can be executed.

Introduction to Classes and Objects CS-2303, C-Term Interfaces versus Implementation Interface –Describes what services a class’s clients can use and how to request those services. without revealing how the class carries out the services. a class definition listing only public member function prototypes. –A class’s interface consists of the class’s public member functions (services). Defined in class header file (.h )

Introduction to Classes and Objects CS-2303, C-Term Interfaces versus Implementation Implementation of member functions –In a separate source-code file for a class Use binary scope resolution operator (::) to tie each member function to the class definition. –Implementation details are hidden. Client code does not need to know the implementation.

Introduction to Classes and Objects CS-2303, C-Term Interfaces versus Implementation (Example) Interface contains data members and member function prototypes

Introduction to Classes and Objects CS-2303, C-Term Interfaces versus Implementation (continued) Binary scope resolution operator ties a function to its class GradeBook implementation is placed in a separate source-code file Include the header file to access the class name GradeBook

Introduction to Classes and Objects CS-2303, C-Term Interfaces versus Implementation (continued)

Introduction to Classes and Objects CS-2303, C-Term Client of the Interface

Introduction to Classes and Objects CS-2303, C-Term Summary Introduced class definitions and objects –Public versus private access into class. Syntax for member functions Syntax data members –Get and Set functions –Constructors & Destructors Placing classes in separate files Separating interface from implementation

Introduction to Classes and Objects CS-2303, C-Term Questions? Reading:– Deitel & Deitel, Chapter 19