Introduction to Classes and Objects CS-2303, C-Term 20101 C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.

Slides:



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

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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 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.
 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
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
 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.
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.
Separate Compilation. A key concept in programming  Two kinds of languages, compilation (C, Pascal, …) and interpretation (Lisp, …, Matlab, Phython,
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
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.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing 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
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.
CLASSES : A DEEPER LOOK Chapter 9 Part I 1. 2 OBJECTIVES In this chapter you will learn: How to use a preprocessor wrapper to prevent multiple definition.
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.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
C++ Class. © 2005 Pearson Addison-Wesley. All rights reserved 3-2 Abstract Data Types Figure 3.1 Isolated tasks: the implementation of task T does not.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
計算機程式語言 Lecture 03-1 國立台灣大學生物機電系 林達德 3 3 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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:
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
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
3 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
3-4-5 Introduction.
Introduction to Classes and Objects
Object Oriented Programming in java
Introduction to Classes and Objects
Separating Interface from Implementation
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

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 4 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 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 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 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 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