260 likes | 370 Views
C++ Classes. OOP?. No, I did not leave the leading P off! What does OOP stand for? Object Oriented Programming Paradigm - a model or a set of rules that define a way of programming The two primary paradigms for programming are: Procedural (what we have been doing)
E N D
OOP? • No, I did not leave the leading P off! • What does OOP stand for? • Object Oriented Programming • Paradigm - a model or a set of rules that define a way of programming • The two primary paradigms for programming are: • Procedural (what we have been doing) • OOP (NEW STUFF!!! C++!!!)
Procedural Procedural Paradigm focuses on the idea that all algorithms in a program are performed with functions and data that a programmer can see, understand (at least that’s the concept!), and change. Process: Create necessary functions and then worry about how to pass the data around inside the program. This paradigm is used in C, Pascal, FORTRAN, etc. More “newer” languages like C++ and Java are moving away from Procedural and moving towards OOP. Harder to initially learn but easier to program in once you understand!
OOP in C++! Object-Oriented Paradigm centers on the idea that all programs can be made up of separate entities called objects. A program built from these objects is called an object-oriented program. An object is responsible for manipulating itself. Information can be passed between objects but one object can not directly modify another object. Example: two people talking. You share information but you can not directly modify the information in someone else’s head!
OOP Terms Class - a definition for an object Constructor - special method for creating (also called instantiating) objects of a class Member variables- descriptive information about objects (like adjectives). These are always private, so they can’t be accessed outside of the class definition code. Member functions - perform actions on objects. Can be a calculation, or an accessor or modifier(mutator). These can be public or private. Encapsulation - the term used to describe the process of hiding the data and the code in an object (also called information hiding)
Other Important facts: • A structis the same as a class, except that all members are public in a struct • Destructor function– called when an object of that class goes out of scope; usually used to free up dynamically allocated storage; use the name of the class preceded by tilde (for example: ~ POINT( ) ) • Copy constructor function– called when • 1. A class object is passed as a value parameter (to create the copy that will be used by the function) • 2. A class object is returned as a function result (to create a copy to be returned) • 3. A class object is used to initialize another, newly defined class object (to copy the value of the existing class object to the new class object)
Why do we do this? • 1) Reusability • Once the class is created and debugged, never have to write the code again! Decreases time spent and potential errors! • 2) Containment • Related classes can be contained within each other which allows projects to be broken into smaller pieces. (Example: class of type car might contain 4 objects of type whee1) • 3) Inheritance • Classes can inherit the properties of another object and then add ADDITIONAL items to the class.
What is the difference? • Try the Is-A/Has-A rule?!! • Examples: • IF the object is a type or kind of another object, use inheritance. A house is a building, so inheritance is used. • If an object has another object as part of the object, use containment. A house has a kitchen, so containment is used.
Classes • Best shown through examples… • Make a new project called point. • Create a header file called point.h
classpoint { public: point(); point(int, int); void setX(int); void setY(int); int getX(); int getY(); void moveHor(int); void moveVer(int); private: int myX; int myY; }; // SEMICOLON!!!! The name of the class is point. This class is used to define points in the Cartesian plane… you know, where you make graphs in Math class
All members are divided into two categories: public and private. The public members, both data elements and functions, can be used anywhere in the program. The private members are hidden within the class and are accessible only to member functions. class point { public: point(); point(int, int); void setX(int); void setY(int); int getX(); int getY(); void moveHor(int); void moveVer(int); private: int myX; int myY; }; The current X and Y coordinates. Only accessible inside the class
default constructor: initialize point to 0, 0 class point { public: point(); point(int, int); void setX(int); void setY(int); int getX(); int getY(); void moveHor(int); void moveVer(int); private: int myX; int myY; }; another constructor: initialize point to x, y constructor – a function that initializes the class object’s data members; has the same name as the class; usually is overloaded Note: Although a constructor is a function, it has no return type! Whenever a class object is defined, the appropriate constructor is called: point p; // declares a point initialized to ( 0, 0 ) point q(0, 10); // declares a point initialized to (0, 10)
Modifier (mutator) - special member function that allows the user to set or change a private data member class point { public: point( ); point(int, int); void setX(int); void setY(int); int getX(); int getY(); void moveHor(int); void moveVer(int); private: int myX; int myY; }; Modifier that sets or changes the X coordinate. Modifier that sets or changes the Y coordinate.
Accessor - special member function that allows the user to access a private data member. class point { public: point(); point(int, int); void setX(int); void setY(int); int getX(); int getY(); void moveHor(int); void moveVer(int); private: int myX; int myY; }; Accessor that returns the X coordinate Accessor that returns the Y coordinate
Other public member functions can also be declared. You could have some that are private too, if you want! class point { public: point(); point(int, int); void setX(const int); void setY(const int); int getX(); int getY(); void moveHor(int); void moveVer(int); private: int myX; int myY; }; Moves the POINT d units to the right or left Moves the POINT d units up or down
Save yourpoint.h file. • Add a new C++ Source File to the project: point.cppThis file should include point.h, and any other includes you will use writing the member functions for the point class. • This file is where you will provide function definitions for your class. • Try writing the function definitions now, without looking ahead in this presentation. After you write each one, you can advance a slide to see if you did it right! Each slide has one or two function definitions on it, and they are in the same order as they are listed inpoint.h
To distinguish definitions of functions that belong to a certain class, their names are prefixed with the class name and the scope resolution symbol :: • //default CONSTRUCTOR point::point() { myX = 0; myY = 0; }
To distinguish definitions of functions that belong to a certain class, their names are prefixed with the class name and the scope resolution symbol :: • //CONSTRUCTOR with arguments for X and Y point::point(int x, int y) { myX = x; myY = y; }
The code for the accessor member functions: int point::getX() { return myX; } int point::getY() { return myY; }
The code for the modifier member functions: void point::setX(int x) { myX=x; } void point::setY(int y) { myY=y; }
The code for the other member functions: void point::moveHor(int d) { myX += d; } void point::moveVer(int d) { myY += d; } You are finished with the point class!! Now save point.cpp!!
Add a new cpp file to this project called testPoint.cpp In this program, create a default point (0, 0) and a point with coordinates determined by the user (prompt the user for x and y). Print out both point coordinates using your accessors. Then, move the user’s point horizontally left 3 units, and down 1 unit by calling your member functions that move points. Now, use your modifiers to reset the coordinates of the default point to be (-5, 8). Print out the coordinates of your points again. You should test all of the functions from your point class with this program!
//need to include point.h and other necessary includes! int main() { point p1;//default constructor int x=0, y=0; cout<<"Enter an x coordinate (integer): "; cin>>x; cout<<"Enter a y coordinate (integer): "; cin>>y; point p2(x, y);//constructor with arguments //using accessors to print coordinatescout<<"Point 1: ("<<p1.getX()<<", "<<p1.getY()<<")"<<endl; cout<<"Point 2: ("<<p2.getX()<<", "<<p2.getY()<<")"<<endl; cout<<"Moving point 2 and resetting point 1...."<<endl; p2.moveHor(-3);//member function to move x p2.moveVer(-1);//member function to move y p1.setX(-5);//member function to change x p1.setY(8);//member function to change y //using accessors to print coordinates again cout<<"Point 1: ("<<p1.getX()<<", "<<p1.getY()<<")"<<endl; cout<<"Point 2: ("<<p2.getX()<<", "<<p2.getY()<<")"<<endl; return 0; } Your testpoint.cpp should look something like this…
To Complete the Creation of the Header File “point.h” (This is only necessary if you plan to use this class in other programs!) Add the following code at the beginning of the file: //header file for class point #ifndef _POINT_H_ #define _POINT_H_ Add the following code at the end of the file: #include "point.cpp" //add this before you //copy to includes folder! #endif //_POINT_H_
Now that you know the class works, it is time to copy your point.h and point.cpp files to your Includes directory so you can reuse your class in future programs.Don’t forget to change your point.h file to include point.cpp!Using another new C++ project create an application called testPoint2.cpp to test this out! Just create and print a few points to make sure the new project recognizes your point class.
Your testpoint2.cpp could look something like this… #include "point.h " #include <iostream> using namespace std; int main() { point p1, p2(3,6); cout<<"Point 1: ("<<p1.getX()<<", "<<p1.getY()<<")"<<endl; cout<<"Point 2: ("<<p2.getX()<<", "<<p2.getY()<<")"<<endl; return 0; }