Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.

Slides:



Advertisements
Similar presentations
Chapter 13 – Introduction to Classes
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Starting Out with C++: Early Objects 5th Edition
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Chapter 6: A First Look at Classes
1 Object-Oriented Programming Using C++ CLASS 27.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Introduction To Classes Chapter Procedural And Object Oriented Programming Procedural programming focuses on the process/actions that occur in a.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Procedural and Object-Oriented Programming 13.1.
1 Chapter 13 Introduction to Classes. 2 Topics 12.1 Procedural and Object-Oriented Programming 12.2 Introduction to Classes 12.3 Defining an Instance.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
C++ Review (3) Structs, Classes, Data Abstraction.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 7: Introduction.
 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.
Structured Data and Classes Chapter 7. Combining Data into Structures Structure: C++ construct that allows multiple variables to be grouped together Structure.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
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.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 12 Introduction to Classes.
1 CSE 2341 Object Oriented Programming with C++ Note Set #5.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Lecture 19: Introduction to Classes Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 3 A First Look at Classes and Objects.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes Procedural and Object-Oriented Programming Procedural programming is a method.
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 Object-Oriented Programming Using C++ CLASS 2 Honors.
1 Class 19 Chapter 13 – Creating a class definition.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley The Unified Modeling Language
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 3: A First Look at Classes and Objects.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Classes C++ representation of an object
Chapter 7: Introduction to Classes and Objects
Abstract Data Types Programmer-created data types that specify
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Review: Two Programming Paradigms
classes and objects review
Introduction to Classes
Chapter 7: Introduction to Classes and Objects
Chapter 5 Classes.
Introduction to Classes
Classes C++ representation of an object
Lecture 8 Object Oriented Programming (OOP)
Presentation transcript:

Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes

Copyright © 2012 Pearson Education, Inc Procedural and Object-Oriented Programming

Copyright © 2012 Pearson Education, Inc. Procedural and Object-Oriented Programming Procedural programming focuses on the process/actions that occur in a program Object-Oriented programming is based on the data and the functions that operate on it. Objects represent the data and its functions.

Copyright © 2012 Pearson Education, Inc. Limitations of Procedural Programming If the data structures change, many functions must also be changed Programs that are based on complex function hierarchies are: –difficult to understand and maintain –difficult to modify and extend

Copyright © 2012 Pearson Education, Inc. Classes and Objects A Class is like a blueprint and objects are like houses built from the blueprint

Copyright © 2012 Pearson Education, Inc. Object-Oriented Programming Terminology attributes: members of a class methods: member functions of a class

Copyright © 2012 Pearson Education, Inc Introduction to Classes

Copyright © 2012 Pearson Education, Inc. Introduction to Classes Objects are created from a class Format: class ClassName { declaration; };

Copyright © 2012 Pearson Education, Inc. Class Example

Copyright © 2012 Pearson Education, Inc. Access Specifiers Used to control access to members of the class public: can be accessed by functions outside of the class private: can only be called by or accessed by functions that are members of the class

Copyright © 2012 Pearson Education, Inc. Class Example Private Members Public Members If not specified, the default access specifier is private

Copyright © 2012 Pearson Education, Inc. Using const With Member Functions const appearing after the parentheses in a member function declaration specifies that the function will not change any data in the calling object.

Copyright © 2012 Pearson Education, Inc. Defining a Member Function When defining a member function: –Put prototype in class declaration –Define function using class name and scope resolution operator (::) int Rectangle::setWidth(double w) { width = w; }

Copyright © 2012 Pearson Education, Inc. Accessors and Mutators Mutator: a member function that stores a value in a private member variable, or changes its value in some way Accessor: function that retrieves a value from a private member variable. Accessors do not change an object's data, so they should be marked const.

Copyright © 2012 Pearson Education, Inc Defining an Instance of a Class

Copyright © 2012 Pearson Education, Inc. Defining an Instance of a Class An object is an instance of a class Defined like structure variables: Rectangle r; Access members using dot operator: r.setWidth(5.2); cout << r.getWidth(); Compiler error if attempt to access private member using dot operator

Copyright © 2012 Pearson Education, Inc.

Program 13-1 (Continued)

Copyright © 2012 Pearson Education, Inc. Program 13-1 (Continued)

Copyright © 2012 Pearson Education, Inc. Program 13-1 (Continued)

Copyright © 2012 Pearson Education, Inc Inline Member Functions

Copyright © 2012 Pearson Education, Inc. Inline Member Functions Member functions can be defined –inline: in class declaration –after the class declaration Inline appropriate for short function bodies: int getWidth() const { return width; }

Copyright © 2012 Pearson Education, Inc. Rectangle Class with Inline Member Functions 1 // Specification file for the Rectangle class 2 // This version uses some inline member functions class Rectangle 7 { 8 private: 9 double width; 10 double length; 11 public: 12 void setWidth(double); 13 void setLength(double); double getWidth() const 16 { return width; } double getLength() const 19 { return length; } double getArea() const 22 { return width * length; } 23 }; 24

Copyright © 2012 Pearson Education, Inc. Tradeoffs – Inline vs. Regular Member Functions Regular functions – when called, compiler stores return address of call, allocates memory for local variables, etc. Code for an inline function is copied into program in place of call – larger executable program, but no function call overhead, hence faster execution

Copyright © 2012 Pearson Education, Inc Constructors

Copyright © 2012 Pearson Education, Inc. Constructors Member function that is automatically called when an object is created Purpose is to construct an object Constructor function name is class name Has no return type

Copyright © 2012 Pearson Education, Inc. Constructors Example #include using namespace std; class CRectangle { int width, height; public: CRectangle (int,int); int area () {return (width*height);} }; CRectangle::CRectangle (int a, int b) { width = a; height = b; } int main () { CRectangle rect (3,4); CRectangle rectb (5,6); cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; return 0; }

Copyright © 2012 Pearson Education, Inc. Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a default constructor for you, one that does nothing. A simple instantiation of a class (with no arguments) calls the default constructor: Rectangle r;

Copyright © 2012 Pearson Education, Inc Destructors

Copyright © 2012 Pearson Education, Inc. Destructors Member function automatically called when an object is destroyed Destructor name is ~ classname, e.g., ~Rectangle(){} Has no return type; takes no arguments

Copyright © 2012 Pearson Education, Inc. Destructor Example #include using namespace std; class Rectangle { public: Rectangle() { cout<<"Constructor invoked"<<endl; } ~Rectangle() { cout<<"Destructor invoked"<<endl; } }; int main () { Rectangle r; cout<<"Main Function"<<endl; return 0; }

Copyright © 2012 Pearson Education, Inc. Inheritance

Copyright © 2012 Pearson Education, Inc. Inheritance Allows us to define a class in terms of another class Provides an opportunity to reuse the code, no need to write from scratch The existing class is the base class and the new class is the derived class Syntax Class derived_class : access_specifier base_class Supports multiple inheritance Example