Presentation is loading. Please wait.

Presentation is loading. Please wait.

Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes Procedural and Object-Oriented Programming Procedural programming is a method.

Similar presentations


Presentation on theme: "Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes Procedural and Object-Oriented Programming Procedural programming is a method."— Presentation transcript:

1 Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes Procedural and Object-Oriented Programming Procedural programming is a method of writing software. It is a programming practice centered on the procedures, or actions that take place in a program. Object-Oriented programming is centered around the object. Objects are created from abstract data types that encapsulate data and functions together.

2 Starting Out with C++, 3 rd Edition 2 What’s Wrong with Procedural Programming? Programs with excessive global data. Complex and convoluted programs. Programs that are difficult to modify and extend.

3 Starting Out with C++, 3 rd Edition 3 What is Object-Oriented Programming? OOP is centered around the object, which packages together both the data and the functions that operate on the data.

4 Starting Out with C++, 3 rd Edition 4 In OOP, an object’s member variables are often called its attributes Member functions are sometimes referred to as its behaviors or methods. Member Variables float width; float length; float area; Member Functions void setData(float w, float l) { … function code … } void calcArea(void) { … function code … } void getWidth(void) { … function code … } void getLength(void) { … function code … } void getArea(void) { … function code … }

5 Starting Out with C++, 3 rd Edition 5 Interface with Object

6 Starting Out with C++, 3 rd Edition 6 General Purpose Objects Creating data types that are improvements on C++’s built-in data types. For example, an array object could be created that works like a regular array, but additionally provides bounds-checking. Creating data types that are missing from C++. For instance, an object could be designed to process currencies or dates as if they were built-in data types. Creating objects that perform commonly needed tasks, such as input validation and screen output in a graphical user interface.

7 Starting Out with C++, 3 rd Edition 7 Application-Specific Objects Data types created for a specific application. For example, in an inventory program. HAVE IT YOUR WAY!

8 Starting Out with C++, 3 rd Edition 8 Introduction to the Class Looks similar to a struct The syntax is: class class-name { // declaration statements here };

9 Starting Out with C++, 3 rd Edition 9 Example Class Declaration Note how methods have been moved inside the brackets. Also, the private data is assumed. class Rectangle { private: float width, length, area; public: void setData ( float, float ); void calcArea ( void ); float getWidth ( void ); float getLength ( void ); float getArea ( void ); }; // Rectangle

10 Starting Out with C++, 3 rd Edition 10 Compare struct code with classes struct Rectangle1 { float width, length, area; } void setData(Rectangle1 & r, float w, float l){ r.width =r; r.length = l; r.area = r*l; }

11 Starting Out with C++, 3 rd Edition 11 With classes Method moves inside Don’t need to pass the Rectangle type in class Rectangle { float width, length, area; void setData(float w, float l){ width =r; length = l; area = r*l; } };

12 Starting Out with C++, 3 rd Edition 12 Notice how we morph a struct into a class However, now the class is getting so huge that it is hard to see what is going on. To make it easier to see just the part the user needs (how to call methods), we pull the implementation of the methods into a separate file. Irritating to have to make the two files match. Design first! Copy the declaration file into the implementation file.

13 Starting Out with C++, 3 rd Edition 13 Access Specifiers The key words private and public are access specifiers. private means the data/methods can only be accessed/called by the member functions. public means the data/methods can be accessed 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 keyword to explicitly declare private members. This clearly documents the access specification of the class.

14 Starting Out with C++, 3 rd Edition 14 Defining Member Functions Class member functions are defined similarly to regular functions. void Rectangle::setData ( float w, float l ) { width = w; length = l; } // setData

15 Starting Out with C++, 3 rd Edition 15 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. For example: Rectangle box; // just like for getting an instance of a struct

16 Starting Out with C++, 3 rd Edition 16 Accessing an Object’s Members For the previous example, to access a member function you would use the statement: box.calcArea(); This is like calling calcArea(box). box is actually the first argument that is passed in. However, we put the class out it front. You have seen that before (e.g. cin.getline(…)) It makes accessing the box’s methods like accessing its variables.

17 Starting Out with C++, 3 rd Edition 17 Pointers to Objects Rectangle box; Rectangle *boxPtr; boxPtr = &box; boxPtr->setData( 15, 12 ); boxPtr->length; // would work if not private

18 Starting Out with C++, 3 rd Edition 18 Rectangle.h class Rectangle {private: float width; float length; float area; public: void setData ( float, float ); void calcArea ( void ); float getWidth ( void ); float getLength ( void ); float getArea ( void ); }; // Rectangle

19 Starting Out with C++, 3 rd Edition 19 Rectangle.cpp notice the inclusion of class name before the method #include “rectangle.h” // Note “” not <> void Rectangle::setData( float w, float l ) {width = w; length = l; } // setData void Rectangle::calcArea ( void ) {area = width * length; } // calcArea

20 Starting Out with C++, 3 rd Edition 20 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. In the class Rectangle, this was done with the getLength, getWidth, and getArea methods. Sounds dumb – but notice how we let the user get the area, but not set it! We make sure it is set correctly. You could make the variables public, and then you wouldn’t need getters/setters, BUT this violates good design principles.

21 Starting Out with C++, 3 rd Edition 21 Some Design Considerations Usually class declarations are stored in their own header files (.h file). Member function definitions are stored in their own.cpp file. This is nice as we can give someone just the public part (.h) and they aren’t burdened with knowing too much. Also, allows separate compilation. One pass compiler can compile something that uses Rectangle without actually seeing it. Makes it more efficient when one file changes – as only recompiles the piece that has changed, not all of it.

22 Starting Out with C++, 3 rd Edition 22 Some Design Considerations The #ifndef directive is called an include guard allows a program to be conditionally compiled. If flag after #ifndef is already known, skip until the #endif. This prevents a header file from accidentally being included more than once – causing you to get multiple copies of things (which sometimes causes problems).

23 Starting Out with C++, 3 rd Edition 23 Rectangle.h #ifndef RECTANGLE_H #define RECTANGLE_H class Rectangle {private: float width; float length; float area; public: void setData ( float, float ); void calcArea ( void ); float getWidth ( void ); float getLength ( void ); float getArea ( void ); }; // Rectangle #endif

24 Starting Out with C++, 3 rd Edition 24 Putting the Files Together Need to include rectangle.h in both rectangle.cpp and main.cpp. Need to compile: –Rectangle.cpp –main.cpp The object files –Rectangle.obj and –main.obj are linked together. Need to have defined a project! Then execute: –Recttest.exe

25 Starting Out with C++, 3 rd Edition 25 Using Private Member Functions A private member function may only be called from a function that is a member of the same object. If try to access a private member, get the message ‘myClassName::getPrivate' : cannot access private member declared in class ‘myClassName‘ Practice making mistakes so you can tell how computer will inform you what you did wrong. I wonder what would happen if …

26 Starting Out with C++, 3 rd Edition 26 Inline Member Functions When the body of a member function is defined inside a class declaration, it is declared inline. Member functions are only defined inside the class declaration if they are small. Avoids call/return in code – no call, actually substituted. Can’t inline code with loops.

27 Starting Out with C++, 3 rd Edition 27 Constructors A constructor is a member function that is automatically called when a class object is instantiated (defined). Constructors have the same name as the class. Constructors must be declared publicly. Constructors have no return type.

28 Starting Out with C++, 3 rd Edition 28 Constructor – Program class Demo { private: int num; public: Demo(void); }; // Demo Demo::Demo ( void ) { num = 0; cout << "Welcome to the Demo constructor!\n"; } // Demo constructor Demo::Demo(int v){ num = v; } void main ( void ) {Demo demoObj; Demo d1(15); cout << "This program demonstrates an object\n"; cout << "with a constructor.\n"; } // main

29 Starting Out with C++, 3 rd Edition 29 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.

30 Starting Out with C++, 3 rd Edition 30 WARNING When you don’t pass a class to a function by reference, both the copy constructor (a constructor whose argument is the same type) and the destructor are called. This can cause some crazy things to happen if the constructor dynamically allocates some memory, and the destructor deletes it.

31 Starting Out with C++, 3 rd Edition 31 Destructors – Program #include class Demo { public: Demo ( void ) { cout << "Welcome to the constructor!\n"; } ~Demo ( void ) { cout << "The destructor is now running.\n“ }; }; // Demo void main ( void ) {Demo demoObj; cout << "This program demonstrates an object\n"; cout << "with a constructor and destructor.\n"; } // main

32 Starting Out with C++, 3 rd Edition 32 Constructor Arguments When a constructor does not have any 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.

33 Starting Out with C++, 3 rd Edition 33 Constructors with Arguments – Program class InvItem {private: char *desc; int units; public: InvItem ( void ) { desc = new char [51]; strcpy( desc, ""); units = 0; } InvItem ( char *dscr, int un ) { desc = new char[51]; strcpy( desc, dscr ); units = un; } ~InvItem ( void ) { delete desc }; char *getDesc ( void ) { return desc; } int getUnits ( void ) { return units; } }; // InvItem

34 Starting Out with C++, 3 rd Edition 34 Only One Default Constructor and One Destructor – as which one is wanted is determined by parameters. A class may only have one default constructor (one without any arguments). A class may only have one destructor.

35 Starting Out with C++, 3 rd Edition 35 Arrays of Objects You may declare and work with arrays of class objects. InvItem inventory[40]; In this case – the default constructor is called for each of the 40 InvItems


Download ppt "Starting Out with C++, 3 rd Edition 1 Chapter 13 – Introduction to Classes Procedural and Object-Oriented Programming Procedural programming is a method."

Similar presentations


Ads by Google