Presentation is loading. Please wait.

Presentation is loading. Please wait.

Andy Wang Object Oriented Programming in C++ COP 3330

Similar presentations


Presentation on theme: "Andy Wang Object Oriented Programming in C++ COP 3330"— Presentation transcript:

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

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

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

12 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; };

13 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; };

14 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

15 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; };

16 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

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

18 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 };

19 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;

20 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; }

21 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?

22 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();

23 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”; }

24 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.

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

26 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

27 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();

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

29 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

30 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

31 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;

32 Error Checking frac2

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

34 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;

35 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); }


Download ppt "Andy Wang Object Oriented Programming in C++ COP 3330"

Similar presentations


Ads by Google