Classes and Objects Andy Wang Object Oriented Programming
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 • 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 <class. Name> { public: // public member data and functions go here private: // private member data and functions go here Remember this semicolon. };
Example: class Circle { public: void Set. Center(double x, double y); void Set. Radious(double r); void Draw(); private: double center_x, center_y, radious; };
Example Time. Type class Time. Type { public: void Set(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 { public: Circle(); // this is a constructor Circle(double r); // this is also a constructor void Set. Center(double x, double y); void Set. Radious(double r); void Draw(); private: double center_x, center_y, radious; };
More on Constructors • A constructor is a member function • 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 circ 1; • Create an object named circ 1 • Runs the Circle() constructor function
Example: Fraction Class • http: //www. cs. fsu. edu/~myers/cop 3330/exampl es/frac • 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 Get. Numerator(); int Get. Denominator(); void Set. Value(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 div. Sign; // assume the use of ‘/’ during input cin >> numerator >> div. Sign >> denominator; } void Fraction: : Show() { cout << numerator << ‘/’ << denominator; } int Fraction: : Get. Numerator() { return numerator; } int Fraction: : Get. Denominator() { return denominator; }
frac. cpp void Fraction: : Set. Value(int n, int d) { // need error checking numerator = n; denominator = d; } double Fraction: : Evaluate() { double n = numerator; double d = denominator; return (n/d); } // convert int to double What’s (int) 1 / (int) 2?
main. cpp #include <iostream> #include “frac. h” using namespace std; int main() { Fraction f 1, f 2, f 3(3, 4), f 4(6); cout << “n” The fraction f 1 is “; f 1. Show(); cout << “n” The fraction f 2 is “; f 2. Show(); cout << “n” The fraction f 3 is “; f 3. Show(); cout << “n” The fraction f 4 is “; f 4. Show();
main. cpp cout << “n Now enter first fraction: “; f 1. Input(); cout << “n. You entered “; , f 1. Show(); cout << “n Now enter second fraction: “; f 2. Input(); cout << “n. You entered “; f 2. Show(); cout << “n The value of fraction 1 is “ << f 1. Evaluate() << ‘n’; cout << “n The value of fraction 2 is “ << f 2. Evaluate() << ‘n’; cout << “Goodbye!n”; }
Remember to use tabs to indent. makefile fraction_executable: frac. o main. o g++ -o frac. o main. o chmod 755 frac Allow frac to be executed as a program. 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 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 • return. Type class. Name: : member. Function. Name • : : 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 f 1, f 2; • To call a member function, the syntax format is • object. Name. member. Function. Name • Examples • f 1. Show(); • cout << f 2. 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 f 1, f 2; • 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 f 1(2, 3) passes 2 and 3 as parameters • Fraction f 3(6) passes in the first value and uses the default value of 1 as the second parameter • int x = 4, y = 8; • Fraction f 2(x, y) passes in the values stored in x and y
Common Pitfalls • Fraction f 1; // will call the default constructor • Fraction f 2(); // compiler will treat it as a function // declaration • f 1. Fraction(); // compiler error • f 1 = Fraction(3, 4); // a fraction of ¾ is created and // copied to f 1;
Error Checking • http: //www. cs. fsu. edu/~myers/cop 3330/exampl es/frac 2
Error Checking: frac. h bool Set. Value(int n, int d);
Error Checking: frac. cpp Fraction: : Fraction(int n, int d) { if (Set. Value(n, d) == false) Set. Value(0, 1); } bool Fraction: : Set. Value(int n, int d) { if (d == 0) { return false } numerator = n; denominator = d; return true; }
Error Checking: frac. cpp void Fraction: : Input() { char div. Sign; do { cin >> numerator >> div. Sign >> denominator; if (denominator == 0) cout << "Illegal Fraction. Try again: "; } while (denominator == 0); }
- Slides: 35