Andy Wang Object Oriented Programming in C++ COP 3330

Slides:



Advertisements
Similar presentations
C++ Classes & Data Abstraction
Advertisements

Operator Overloading Programming in C++ Fall 2008 Dr. David A. Gaitros
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
1 Lab Session-XII CSIT121 Fall 2000 b Namespaces b Will This Program Compile ? b Master of Deceit b Lab Exercise 12-A b First Taste of Classes b Lab Exercise.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
C++ Classes & Object Oriented Programming. Object Oriented Programming  Programmer thinks about and defines the attributes and behavior of objects. 
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
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.
CSC241 Object-Oriented Programming (OOP) Lecture No. 4.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
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 Introduction to Classes and Objects Definitions Examples.
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.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Matthew Small Introduction to Object-Oriented Programming.
1 Introduction to Object Oriented Programming Chapter 10.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Object Oriented Programming COP3330 / CGS5409.  Classes & Objects  DDU Design  Constructors  Member Functions & Data  Friends and member functions.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
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.
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 12 Classes and Abstraction
Procedural and Object-Oriented Programming
Introduction to C++ (Extensions to C)
Chapter Topics The Basics of a C++ Program Data Types
Classes C++ representation of an object
Review What is an object? What is a class?
Introduction to C++ Systems Programming.
Compilation and Debugging
Compilation and Debugging
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Basic Elements of C++.
Andy Wang Object Oriented Programming in C++ COP 3330
Review: Two Programming Paradigms
Chapter 5 Classes.
Chapter Structured Types, Data Abstraction and Classes
CMPE Data Structures and Algorithms in C++ February 22 Class Meeting
Basic Elements of C++ Chapter 2.
Object Oriented Programming COP3330 / CGS5409
C++ Classes & Object Oriented Programming
More on Classes Programming in C++ Fall 2008
Andy Wang Object Oriented Programming in C++ COP 3330
Separate Compilation and Namespaces
Arrays and Classes Programming in C++ Fall 2008
Object-Oriented Programming
Andy Wang Object Oriented Programming in C++ COP 3330
Learning Objectives Classes Constructors Principles of OOP
Introduction to Classes and Objects
Classes and Objects.
Object-Oriented Programming
UNIT I OBJECT ORIENTED PROGRAMMING FUNDAMENTALS
COP 3330 Object-oriented Programming in C++
COP 3330 Object-oriented Programming in C++
Object-Oriented Programming
Defining Classes and Methods
Andy Wang Object Oriented Programming in C++ COP 3330
Classes C++ representation of an object
CS 144 Advanced C++ Programming February 21 Class Meeting
Class rational part2.
Object Oriented programming
CECS 130 Final Exam Review Session
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

Andy Wang Object Oriented Programming in C++ COP 3330 Classes and Objects Andy Wang Object Oriented Programming in C++ COP 3330

Object Encapsulation of data and functions that act upon that data An object consists of Name (variable name) Attributes (member data) that describe what the object is Behavior (member functions) that describes what the object does

Class A blueprint for objects A user-defined type, consists of Declaration (typically in .h files) Definition (typically in .cpp files) An object is an instance of a class Can create many objects from the same class Can build many houses from the same blueprint

DDU Design—Declare, Define, Use A declaration gives an interface A variable declaration specifies the type A function declaration tells how to use it Not how it works A class declaration shows what an object will look like and what its available functions are No implementation details

DDU Design—Declare, Define, Use A definition consists of the implementation details The user of the interface will not see this part A function definition is the code that makes the function work A class definition consists of definitions of its member functions

DDU Design—Declare, Define, Use The use of an item through its interface The user of an program uses the graphical user interface, keyboard, and mouse The user of a function is a programmer, who makes calls to the function (without knowing the implementation details) The user of a class is a programmer, who uses the class by creating objects and calling available member functions of those objects

Interface What user sees Implementation details are hidden Not necessary the user of a program Could be a programmer (user of a class) Implementation details are hidden

Protection Levels in a Class Members of a class can be public, private, etc. Public Can be accessed from inside or outside of the object Is essentially the interface of the object (need to be simple) The user is some other portion of code (other classes, functions, main program) Want to provide functions that handle all necessary actions on the object

Protection Levels in a Class Private Can only be used by the object itself Standard practice to protect member data of a class Same for helper functions that do not need to be part of the interface

Reasons for Data Hiding Simpler interface Principle of least privilege (need-to-know) More secure Less chance of accidental or malicious misuse E.g., front wheels of a car should turn in the same direction Easier to change class implementation without affecting other modules that use it

Class Declaration Format class <className> { public: // public member data and functions go here private: // private member data and functions go here }; Remember this semicolon.

Example: class Circle class Circle { public: void SetCenter(double x, double y); void SetRadious(double r); void Draw(); private: double center_x, center_y, radious; };

Example TimeType class TimeType { public: void Set(int, int, int); // set the time void Increment(); // increment by one sec void Display(); // output the time private: int hours, minutes, seconds; };

Constructors Special member function of class Usually used to initialize the members of object Has the same name as the class Has no return type

Example: class Circle class Circle { public: Circle(); // this is a constructor Circle(double r); // this is also a constructor void SetCenter(double x, double y); void SetRadious(double r); void Draw(); private: double center_x, center_y, radious; };

More on Constructors A constructor is a member function Circle circ1; You can define anything you want You do not call the constructor function as a member function It is automatically called when you declare an object Circle circ1; Create an object named circ1 Runs the Circle() constructor function

Example: Fraction Class Directory content frac.cpp // class definition frac.h // class declaration main.cpp // driver program to use the class makefile

frac.h class Fraction { public: Fraction(); // set numerator = 0, denominator = 1 Fraction(int n, int d = 1); // constructor with parameters void Input(); // input a fraction from keyboard void Show(); // display a fraction on screen int GetNumerator(); int GetDenominator(); void SetValue(int n, int d); // set the fraction’s value double Evaluate(); // return the decimal value private: int numerator, denominator; // denominator != 0 };

frac.cpp #include <iostream> #include “frac.h” using namespace std; Fraction::Fraction() { // default constructor numerator = 0; denominator = 1; } Fraction::Fraction(int n, int d) { // need error checking numerator = n; denominator = d;

frac.cpp void Fraction::Input() { // need error checking char divSign; // assume the use of ‘/’ during input cin >> numerator >> divSign >> denominator; } void Fraction::Show() { cout << numerator << ‘/’ << denominator; int Fraction::GetNumerator() { return numerator; } int Fraction::GetDenominator() { return denominator; }

frac.cpp void Fraction::SetValue(int n, int d) { // need error checking numerator = n; denominator = d; } double Fraction::Evaluate() { double n = numerator; // convert int to double double d = denominator; // convert int to double return (n/d); What’s (int) 1 / (int) 2?

main.cpp #include <iostream> #include “frac.h” using namespace std; int main() { Fraction f1, f2, f3(3,4), f4(6); cout << “\n” The fraction f1 is “; f1.Show(); cout << “\n” The fraction f2 is “; f2.Show(); cout << “\n” The fraction f3 is “; f3.Show(); cout << “\n” The fraction f4 is “; f4.Show();

main.cpp cout << “\n Now enter first fraction: “; f1.Input(); cout << “\nYou entered “;, f1.Show(); cout << “\n Now enter second fraction: “; f2.Input(); cout << “\nYou entered “; f2.Show(); cout << “\n The value of fraction 1 is “ << f1.Evaluate() << ‘\n’; cout << “\n The value of fraction 2 is “ << f2.Evaluate() cout << “Goodbye!\n”; }

makefile Remember to use tabs to indent. fraction_executable: frac.o main.o g++ -o frac frac.o main.o chmod 755 frac frac.o: frac.cpp frac.h g++ -c frac.cpp main.o: main.cpp frac.h g++ -c main.cpp clean: rm -f *.o frac Allow frac to be executed as a program. Type ‘make clean’ to clean up the temporary files.

Definitions vs. Declarations frac.cpp defines member functions void Fraction::Show() { cout << numerator << ‘/’ denominator; } frac.h declares this function void Show();

Syntax of Definitions returnType className::memberFunctionName :: is called the scope resolution operator Specifying to which class a member function belongs Example: void Fraction::Show() In main.cpp, by using namespace std, we can type cout instead of std::cout

Syntax of Using Member Functions To create objects of type Fraction Fraction f1, f2; To call a member function, the syntax format is objectName.memberFunctionName Examples f1.Show(); cout << f2.Evaluate();

Constructor with Parameters Constructor declarations Fraction(); // default constructor Fraction(int n, int d = 1); // constructor with parameters

Constructor with Parameters Default constructor will always refer to a constructor with no parameters Fraction f1, f2; If a class has no constructor defined, a default constructor will be automatically created If there are constructors, no default constructor will be generated automatically

Constructor with Parameters To use a constructor with parameters, just pass arguments when the object is declared Fraction f1(2,3) passes 2 and 3 as parameters Fraction f3(6) passes in the first value and uses the default value of 1 as the second parameter int x = 4, y = 8; Fraction f2(x, y) passes in the values stored in x and y

Common Pitfalls Fraction f1; // will call the default constructor Fraction f2(); // compiler will treat it as a function // declaration f1.Fraction(); // compiler error f1 = Fraction(3, 4); // a fraction of ¾ is created and // copied to f1;

Error Checking http://www.cs.fsu.edu/~myers/cop3330/examples/ frac2

Error Checking: frac.h bool SetValue(int n, int d);

Error Checking: frac.cpp Fraction::Fraction(int n, int d) { if (SetValue(n,d) == false) SetValue(0,1); } bool Fraction::SetValue(int n, int d) { if (d == 0) { return false } numerator = n; denominator = d; return true;

Error Checking: frac.cpp void Fraction::Input() { char divSign; do { cin >> numerator >> divSign >> denominator; if (denominator == 0) cout << "Illegal Fraction. Try again: "; } while (denominator == 0); }