Object Oriented Programming Using C CLASS 2 Honors 1 Objectives Create a class definition Use a constructor and destructor Differentiate between class interface and implementation Write a main driver to test set and get methods of a class 2 Introduction to the Class In C the class is the construct primarily used to create objects class class name declaration statements here 3 Example class Rectangle private float Width Length Area public void SetData float float void CalcArea void float GetWidth void float GetLength void float GetArea void 4 Access Specifiers The key words private and public are access specifiers private means they can only be accessed by the member functions public means they can be called from statements outside the class Note the default access of a class is private but it is still a good idea to use the private key word to explicitly declare private members This clearly documents the access specification of the class 5 Defining Member Functions Class member functions are defined similarly to regular functions void Rectangle SetData float W float L Width W Length L 6 Defining an Instance of a Class Class objects must be defined after the class is declared Defining a class object is called the instantiation of a class Rectangle Box Box is an instance of Rectangle 7 Accessing an Object s Members Box CalcArea 8 Why Have Private Members In object oriented programming an object should protect its important data by making it private and providing a public interface to access that data 9 Focus on Software Engineering Some Design Considerations Usually class declarations are stored in their own header files Member function definitions are stored in their own CPP files The ifndef directive allows a program to be conditionally compiled This prevents a header file from accidentally being included more than once 10 Contents of Rectangle h ifndef RECTANGLE H define RECTANGLE H Rectangle class declaration class Rectangle private float width float length 11 public bool setWidth float bool setLength float float getWidth void float getLength void float getArea void endif 12 Contents of Rectangle CPP include rectang h SetWidth copies the argument w to private member width if the argument is valid if argument is negative private member width is set to 0 0 and a false returned bool Rectangle setWidth float w bool status if w 0 width 0 0 status false else width w status true return status 13 Contents of Rectangle CPP include rectang h SetWidth copies the argument len to private member length if the argument is valid if argument is negative private member length is set to 0 0 and a false returned bool Rectangle setLength float len bool status if l 0 length 0 0 status false else length len status true return status 14 Program continues GetWidth returns the value in the private member Width float Rectangle getWidth void return width GetLength returns the value in the private member Length float Rectangle getLength void return length GetArea returns the value in the private member Area float Rectangle getArea void return width length 15 This program demonstrates a simple class include iostream include rectang h contains Rectangle class declaration using namespace std Don t forget to link this program with Rectangle cpp int main Rectangle Box float rectWidth rectLength cout This program will calculate the area of a n cout rectangle What is the width cin rectWidth cout What is the length cin rectLength 16 Program continues if Box setWidth rectWidth cout invalid value for width else if if Box setLength rectLength cout invalid value for length else cout Here rectangle s data n cout Width Box getWidth endl cout Length Box getLength endl cout Area Box getArea endl return 0 17 Performing I O in a Class Object Notice that the Rectangle example has no cin or cout This is so anyone who writes a program that uses the Rectangle class will not be locked into the way the class performs input or output Unless a class is specifically designed to perform I O operations like user input and output are best left to the person designing the application 18 Rectangle h Rectangle cpp Pr13 3 cpp Contains the declaration of the Rectangle class Contains the Rectangle class s member function definitions Contains functiton main 19 Focus on Software Engineering Using Private Member Functions A private member function may only be called from a function that is a member of the same object 20 Inline Member Functions When the body of a member function is defined inside a class declaration it is declared inline 21 Rectangle class declaration with some functions written inline class Rectangle private float width float length public bool setWidth float not inlined would be defined in cpp file bool setLength float float getWidth return width inlined would not be in cpp float getLength return length float getArea return width length 22 Constructors A constructor is a member function that is automatically called when a class object is created Constructors have the same name as the class Constructors must be declared publicly Constructors have no return type 23 This program demonstrates a constructor include iostream using namespace std class Demo public Demo void Constructor Demo Demo void cout Welcome to the constructor n 24 Program continues int main Demo DemoObj Declare a Demo object cout This program demonstrates an object n cout with a constructor n return 0 25 Program Output Welcome to the constructor This program demonstrates an object with a constructor 26 Constructor Arguments When a constructor does not have to accept arguments it is called an object s default constructor Like regular functions constructors may accept arguments have default arguments be declared inline and be overloaded 27 Destructors A destructor is a member function that is automatically called when an object is destroyed Destructors have the same name as the class preceded by a tilde character In the same way that a constructor is called then the object is created the destructor is automatically called when the object is destroyed In the same way that a constructor sets things up when an object is created a destructor performs shutdown procedures when an object is destroyed 28 This program demonstrates a constructor include iostream using namespace std class Demo public Demo void Constructor Demo void Destructor Demo Demo void cout Welcome to the constructor n 29 Program continues Demo Demo void cout The destructor is now running n int main
View Full Document
Unlocking...