UT Dallas CS 4337 - Chapter13 Class1 - OOP access specifier, constructor destructor - July 7, 2015 (82 pages)

Previewing pages 1, 2, 3, 4, 5, 39, 40, 41, 42, 43, 44, 78, 79, 80, 81, 82 of 82 page document View the full content.
View Full Document

Chapter13 Class1 - OOP access specifier, constructor destructor - July 7, 2015



Previewing pages 1, 2, 3, 4, 5, 39, 40, 41, 42, 43, 44, 78, 79, 80, 81, 82 of actual document.

View the full content.
View Full Document
View Full Document

Chapter13 Class1 - OOP access specifier, constructor destructor - July 7, 2015

36 views


Pages:
82
School:
University of Texas at Dallas
Course:
Cs 4337 - Organization of Programming Languages
Unformatted text preview:

Chapter 13 Introduction to Classes Copyright 2012 Pearson Education Inc 13 1 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 are instances of ADTs that 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 easy to break Copyright 2012 Pearson Education Inc Object Oriented Programming Terminology class like a struct allows bundling of related variables but variables and functions in the class can have different properties than in a struct object an instance of a class in the same way that a variable can be an instance of a struct Copyright 2012 Pearson Education Inc 7 07 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 or behaviors member functions of a class Copyright 2012 Pearson Education Inc More on Objects data hiding restricting access to certain members of an object public interface members of an object that are available outside of the object This allows the object to provide access to some data and functions without sharing its internal details and design and provides some protection from data corruption Copyright 2012 Pearson Education Inc 13 2 Introduction to Classes Copyright 2012 Pearson Education Inc Introduction to Classes Objects are created from a class Format class ClassName declaration 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 Copyright 2012 Pearson Education Inc More on Access Specifiers Can be listed in any order in a class Can appear multiple times in a class If not specified the default 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 getter setter 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 13 3 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 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 Avoiding Stale Data Some data is the result of a calculation In the Rectangle class the area of a rectangle is calculated length x width If we were to use an area variable here in the Rectangle class its value would be dependent on the length and the width If we change length or width without updating area then area would become stale To avoid stale data it is best to calculate the value of that data within a member function rather than store it in a variable Copyright 2012 Pearson Education Inc Pointer to an Object Can define a pointer to an object Rectangle rPtr Can access public members via pointer rPtr otherRectangle rPtr setLength 12 5 cout rPtr getLength endl Copyright 2012 Pearson Education Inc Dynamically Allocating an Object We can also use a pointer to dynamically allocate an object Copyright 2012 Pearson Education Inc 13 4 Why Have Private Members Copyright 2012 Pearson Education Inc Why Have Private Members Making data members private provides data protection Data can be accessed only through public functions Public functions define the class s public interface Copyright 2012 Pearson Education Inc Code outside the class must use the class s public member functions to interact with the object Copyright 2012 Pearson Education Inc 13 5 Separating Specification from Implementation Copyright 2012 Pearson Education Inc Separating Specification from Implementation Place class declaration in a header file that serves as the class specification file Name the file ClassName h for example Rectangle h Place member function definitions in ClassName cpp for example Rectangle cpp File should include the class specification file Programs that use the class must include the class specification file and be compiled and linked with the member function definitions Copyright 2012 Pearson Education Inc 13 6 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 Specification file for the Rectangle class This version uses some inline member functions ifndef RECTANGLE H define RECTANGLE H class Rectangle private double width double length public void setWidth double void setLength double double getWidth const return width double getLength const return length double getArea const return width length endif Copyright 2012 Pearson Education Inc


View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view Chapter13 Class1 - OOP access specifier, constructor destructor - July 7, 2015 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter13 Class1 - OOP access specifier, constructor destructor - July 7, 2015 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?