Chapter 12: Classes and Data Abstraction

Slides:



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

Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
Chapter 14: Overloading and Templates
Chapter 11: Records (structs)
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 11: Classes and Data Abstraction.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
Chapter 8 User-Defined Classes and ADTs. Chapter Objectives Learn about classes Learn about private, protected, public, and static members of a class.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter 11: Classes and Data Abstraction
Chapter 8: User-Defined Classes and ADTs J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 15: Overloading and Templates.
CLASSES AND DATA ABSTRACTION
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Chapter 11: Classes and Data Abstraction. C++ Programming: Program Design Including Data Structures, Fourth Edition2 Objectives In this chapter, you will:
CHAPTER 13 CLASSES AND DATA ABSTRACTION. In this chapter, you will:  Learn about classes  Learn about private, protected, and public members of a class.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Data Structures Using C++1 Chapter 1 -Software Engineering Principles -ADT and Classes.
Chapter 8: User-Defined Classes and ADTs
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
CS1201: Programming Language 2 Classes and objects By: Nouf Aljaffan Edited by : Nouf Almunyif.
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.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 8: User-Defined Classes and ADTs J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 12: Classes and Data Abstraction.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Object-Oriented Programming in C++ Lecture 4 Constants References Operator overloading.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 11: Classes and Data Abstraction.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 12: Classes and Data Abstraction.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 11: Classes and Data Abstraction.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 05: Classes and Data Abstraction.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 11: Classes and Data Abstraction.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
1 CS 132 Spring 2008 Chapter 1 Software Engineering Principles and C++ Classes.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Object-Oriented Programming Using C++ Third Edition Chapter 7 Using Classes.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Structures and Classes
Chapter 7: User-Defined Functions II
Java Programming: Guided Learning with Early Objects
About the Presentations
Chapter 15: Overloading and Templates
User-Defined Functions
User-Defined Classes and ADTs
Classes and Data Abstraction
Chapter 8: User-Defined Classes and ADTs
User-Defined Classes and ADTs
Chapter 8 Classes User-Defined Classes and ADTs
Chapter 7: User-Defined Functions II
Chapter 12: Classes and Data Abstraction
Presentation transcript:

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

Objectives In this chapter you will: Learn about classes Learn about private, protected, and public members of a class Explore how classes are implemented C++ Programming: From Problem Analysis to Program Design, Third Edition

Classes Class: collection of a fixed number of components The components of a class are called members The general syntax for defining a class: C++ Programming: From Problem Analysis to Program Design, Third Edition

Classes (continued) Class member can be a variable or a function If a member of a class is a variable It is declared like any other variable In the definition of the class Cannot initialize a variable when you declare it If a member of a class is a function Function prototype is listed Function members can (directly) access any member of the class C++ Programming: From Problem Analysis to Program Design, Third Edition

Classes (continued) class is a reserved word Class defines a data type, no memory is allocated Don’t forget the semicolon after the closing brace of the class C++ Programming: From Problem Analysis to Program Design, Third Edition

Classes (continued) Three categories of class members private public protected By default, all members of a class are private If a member of a class is private It cannot be accessed outside the class C++ Programming: From Problem Analysis to Program Design, Third Edition

Classes (continued) A public member is accessible outside the class To make a member of a class public Use the label public with a colon private, protected, and public are reserved words C++ Programming: From Problem Analysis to Program Design, Third Edition

The class clockType has seven member functions: setTime, getTime, printTime, incrementSeconds, incrementMinutes, incrementHours, and equalTime. It has three member variables: hr, min, and sec. The three member variables—hr, min, and sec—are private to the class and cannot be accessed outside the class. The seven member functions—setTime, getTime, printTime, incrementSeconds, incrementMinutes, incrementHours, and equalTime—can directly access the member variables (hr, min, and sec). In the function equalTime, the formal parameter is a constant reference parameter. That is, in a call to the function equalTime, the formal parameter receives the address of the actual parameter, but the formal parameter cannot modify the value of the actual parameter. The word const at the end of the member functions getTime, printTime, and equalTime specifies that these functions cannot modify the member variables of a variable of type clockType.

Variable (Object) Declaration Once a class is defined, you can declare variables of that type In C++ terminology, a class variable is called a class object or class instance The syntax for declaring a class object is the same as for declaring any other variable clockType myClock; clockType yourClock; C++ Programming: From Problem Analysis to Program Design, Third Edition

Accessing Class Members Once an object is declared It can access the public members of the class Syntax to access class members: The dot (. ) is called the member access operator C++ Programming: From Problem Analysis to Program Design, Third Edition

Accessing Class Members (continued) The class members that a class object can access depend on where the object is declared. If the object is declared in the definition of a member function of the class, then the object can access both the public and private members. If the object is declared elsewhere (for example, in a user’s program), then the object can access only the public members of the class. C++ Programming: From Problem Analysis to Program Design, Third Edition

Example: 1 // don’t forget to: #include <iostream> #include<string> using namespace std; class student { private: string name; int grade; public: void setname() { cout<<"enter the st name:"; cin>>name; } void printname(){ cout<<"the st name is:"<<name; } void setgrade(){ cout<<"enter the grade: "; cin>>grade; grade=grade+5; } void printgrade(){ cout<<" st grade is: "<<grade<<endl; } }; C++ Programming: From Problem Analysis to Program Design, Third Edition

Example: 1 (continue) int main() { student a, b; a.setname(); a.setgrade(); b=a; a.printname(); a.printgrade(); b.printname(); b.printgrade(); return 0; } Note: cout<<a.grade; is an illegal statement inside the main function, because grade is a private member of class a and can not be access outside the class scope. Output: enter the st name:Omar enter the grade: 88 the st name is:Omar st grade is: 93 C++ Programming: From Problem Analysis to Program Design, Third Edition

Functions and Classes Objects can be passed as parameters to functions and returned as function values As parameters to functions Objects can be passed by value or by reference If an object is passed by value Contents of data members of the actual parameter are copied into the corresponding data members of the formal parameter C++ Programming: From Problem Analysis to Program Design, Third Edition

In order to reference these identifiers, we use the scope resolution operator, :: (double colon). In the function definition’s heading, the name of the function is the name of the class, followed by the scope resolution operator, followed by the function name.

Another way to rewrite Example 1: using namespace std; class student { private: string name; int grade; public: void setname(); void printname(); void setgrade(); void printgrade(); }; void student::setname() { cout<<"enter the st name:"; cin>>name; } void student::printname() { cout<<"the st name is:"<<name; } void student::setgrade() { cout<<"enter the grade: "; cin>>grade; grade= grade+5; } void student::printgrade() { cout<<" st grade is: "<<grade<<endl; } C++ Programming: From Problem Analysis to Program Design, Third Edition

Another way to rewrite Example 1: void readdata(student& temp) { temp.setname(); temp.setgrade(); } void writedata(student temp ) temp.printname(); temp.printgrade(); int main() { student a, b; readdata(a); b=a; writedata(a); writedata(b); return 0; Output: enter the st name:Omar enter the grade: 88 the st name is:Omar st grade is: 93 C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition

To call this function:

Suppose that myClock and yourClock are objects of type clockType, as declared previously. Further suppose that we have myClock and yourClock as shown in Figure 12-7.

Within the definition of this function, the object otherClock accesses the member variables hr, min, and sec. However, these member variables are private. So is there any violation? The answer is no. The function equalTime is a member of the class clockType and hr, min, and sec are the member variables. otherClock is an object of type clockType. Therefore, the object otherClock can access its private member variables within the definition of the function equalTime.

Once a class is properly defined and implemented, it can be used in a program. A program or software that uses and manipulates the objects of a class is called a client of that class. When you declare objects of the class clockType, every object has its own copy of the member variables hr, min, and sec. In object-oriented terminology, variables such as hr, min, and sec are called instance variables of the class because every object has its own instance of the data.

Order of public and private Members of a Class C++ has no fixed order in which you declare public and private members By default all members of a class are private Use the member access specifier public to make a member available for public access C++ Programming: From Problem Analysis to Program Design, Third Edition

Example 12-3

Example 12-4

Example 12-5

Summary Class: collection of a fixed number of components Members: components of a class Members are accessed by name Members are classified into one of three categories: private, protected, and public Class variables are called class objects or, simply, objects C++ Programming: From Problem Analysis to Program Design, Third Edition

Summary (continued) The only built-in operations on classes are the assignment and member selection C++ Programming: From Problem Analysis to Program Design, Third Edition