1 / 97

C++ Classes How to Create and Use Them

C++ Classes How to Create and Use Them. Overview. Terminology File Topology Designing Classes Functions in Classes (methods) Constructor Accessors/Modifiers Miscellaneous The Driver and Object instantiation. Terminology. Now in OO land (Object-Oriented) – Real C++ programming!

erna
Download Presentation

C++ Classes How to Create and Use Them

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ ClassesHow to Create and Use Them

  2. Overview • Terminology • File Topology • Designing Classes • Functions in Classes (methods) • Constructor • Accessors/Modifiers • Miscellaneous • The Driver and Object instantiation

  3. Terminology • Now in OO land (Object-Oriented) – Real C++ programming! • A class is just a combination of data and functions • It creates a new data type! • A class is a generic description • Create several instances of this class (objects) • Each instance has their own variables (they don’t share) • Functions are now called methods or behaviors (and rarely member functions)

  4. File Topology(separate files for everything) Cat.cpp Dog.cpp Description of a cat Description of a dog Driver.cpp This is where we create instances of dog and cats and then tell them what to do. It’s the controlling algorithm Person.cpp Description of a person

  5. A couple of rules • Classes usually begin with an upper-case letter • The name of the file that you put the class in should be called the • <class name>.cpp • Especially if it’s a public class (later) • Example: • If we are creating a dog, we should name the class Dog and put it into a file named Dog.cpp • Remember, C++ is case-sensitive!

  6. The Smallest Class • Has template: class <class name> { }; • Example: you’re told to create class X • Solution: class X { }; • Remember, this should go in a separate file named X.cpp (not x.cpp)

  7. A “real life” example • The CDog • Attributes (characteristics) • rabid or not rabid (bool) • weight (int or float) • name (char [ ]) • Behaviors • growl • eat

  8. Step 1: The Skeleton • class CDog { • // attributes will go here – name, weight, rabid • // behaviors will go here – growl, eat • };

  9. Step 2: The attributes(we’ll discuss public later) • class CDog { • public: • boolean rabid; • int weight; • char name[255]; • // Behaviors go here • };

  10. Step 3: The Constructor • This is a special function • Used to give initial values to ALL attributes • Is activated when someone creates a new instance of the class • The name of this function MUST be the same name as the class

  11. Step 3: Designing the Constructor • Constructors will vary, depending on design • Ask questions: • Are all CDogs born either rabid or non-rabid? (yes – they are all born non-rabid) • Are all CDogs born with the same weight? (no – they are born with different weights) • Are all CDogs born with the same name? (no – they all have different names) • If ever “no”, then you need information passed in as parameters.

  12. Step 3: The Constructor • class CDog { • public: • boolean rabidOrNot; • int weight; • char name [255]; • // Constructor • CDog::CDog (int x, String y) { • rabid = false; • weight = x; • strcpy (name, y); • } • // Behaviors go here • }; Notice that every CDog we create will be born non-rabid. The weight and name will depend on the values of the parameters

  13. The Driver • We’re talking about a whole new file! • The Driver contains main() • It’s the controlling algorithm • Steps: • Type in the skeleton • Create a couple of instances of classes • Start telling them what to do

  14. Step 1: Type in the Skeleton #include <iostream.h> void main ( ) { // Create your instances here! }

  15. Step 2: Declare A CDog Pointer #include <iostream.h> void main ( ) { CDog* c1; } • // null means “dead” Memory null c1

  16. The new operator • new • Brings instances “to life” • Calls the class’ constructor • Opens up enough space in memory to fit an instance of that class • Format: <class>* <var_name>; <var_name> = new <class> (<parameters>);

  17. Step 3: Bring c1 to Life #include <iostream.h> void main ( ) { CDog* c1; c1 = new CDog (14, “Bob”); } Memory null c1 • new calls the CDog’s constructor • CDog::CDog (int x, char y[ ]) { • rabid = false; • weight = x; • strcpy (name, y); • }

  18. Opens Space #include <iostream.h> void main ( ) { CDog* c1; c1 = new CDog (14, “Bob”); } Memory null c1 • new calls the CDog’s constructor • CDog::CDog (int x, char y[ ]) { • rabid = false; • weight = x; • strcpy (name, y); • }

  19. Data Passing #include <iostream.h> void main ( ) { CDog* c1; c1 = new CDog (14, “Bob”); } Memory null c1 // This is over in CDog.cpp • CDog::CDog (int x, char y[ ]) { • rabid = false; • weight = x; • strcpy (name, y); • }

  20. Data Passing #include <iostream.h> void main ( ) { CDog* c1; c1 = new CDog (14, “Bob”); } Memory null c1 // This is over in CDog.cpp • CDog::CDog (int x, char y[ ]) { • rabid = false; • weight = x; • strcpy (name, y); • } rabid:false weight:14 name:”bob”

  21. Declare a 2nd CDog #include <iostream.h> void main ( ) { CDog* c1; c1 = new CDog (14, “Bob”); CDog c2 (7, “Ethel”); } Memory null c1 rabid:false // This is over in CDog.cpp • CDog::CDog (int x, char y[ ]) { • rabid = false; • weight = x; • strcpy (name, y); • } weight:14 name:”bob”

  22. Opens Space #include <iostream.h> void main ( ) { CDog* c1; c1 = new CDog (14, “Bob”); CDog c2 (7, “Ethel”); } Memory null c1 c2 // This is over in CDog.cpp • CDog::CDog (int x, char y[ ]) { • rabid = false; • weight = x; • strcpy (name, y); • } rabid:false weight:14 name:”bob”

  23. Data Passing #include <iostream.h> void main ( ) { CDog* c1; c1 = new CDog (14, “Bob”); CDog c2 (7, “Ethel”); } Memory null c1 c2 // This is over in CDog.cpp rabid:false • CDog::CDog (int x, char y[ ]) { • rabid = false; • weight = x; • strcpy (name, y); • } weight:14 name:”bob”

  24. Copy Values to Memory #include <iostream.h> void main ( ) { CDog* c1; c1 = new CDog (14, “Bob”); CDog c2 (7, “Ethel”); } Memory null c1 c2 // This is over in CDog.cpp rabid:false rabid:false • CDog::CDog (int x, char y[ ]) { • rabid = false; • weight = x; • strcpy (name, y); • } weight:14 weight:7 name:”bob” name:”Ethel”

  25. Back to CDog • class CDog { • public: • boolean rabidOrNot; • int weight; • char name [255]; • // Constructor • CDog::CDog (int x, char y[ ]) { • rabid = false; • weight = x; • strcpy (name, y); • } • // Behaviors we still need to eat and growl • };

  26. Miscellaneous Methods • Follow the pattern void CDog::eat ( ) { cout << name << “ is now eating” << endl; weight++; } void CDog::growl ( ) { cout << “Grrrr” << endl; }

  27. Add Methods • class CDog { • public: • boolean rabidOrNot; • int weight; • char name [255]; • // Constructor • CDog::CDog (int x, char y[ ]) { • rabid = false; • weight = x; • strcpy (name, y); • } • void CDog::eat ( ) { cout << name << “ is now eating” << endl; weight++; } void CDog::growl ( ) { cout << “Grrrr” << endl; } • };

  28. The “.” and “->” operators • “Dot” operator used for non-pointers to: • Get to an instances attributes • Get to an instances methods • Basically get inside the instance • Format: <instance>.<attribute or method> • Arrow operator used for pointers • Format: <instance> -> <attribute or method>

  29. Using the “.” and “->” Operators #include <iostream.h> void main ( ) { CDog* c1; c1 = new CDog (14, “Bob”); CDog c2 (7, “Ethel”); c2.bark( ); c1->growl( ); }

  30. Accessors and Modifiers • Accessors • A method that returns the value of an attribute • Will have non-void return type • Has return statement inside • Modifiers • A method that changes the value of an attribute • Change is based off of a parameter value • Has a void return type • Have one accessor and one modifier per attribute

  31. Accessors and Modifiers • Accessor for the rabid attribute bool CDog::getRabid ( ) { return rabid; } • Modifier for the rabid attribute void CDog::setRabid (bool myBoolean) { rabid = myBoolean; } • Put these inside of the CDog class

  32. Using accessors and modifiers #include <iostream.h> void main ( ) { CDog* c1; c1 = new CDog (14, “Bob”); CDog c2 (7, “Ethel”); c1->setRabid (1); // prints 1 for true cout << c1->getRabid( ) << endl; }

  33. Make a Separate Header File(for the generic description) class CDog { public: int weight; bool rabid; char name [ ]; CDog (int x, char y[ ]); bool getRabid ( ); void setRabid (bool x); char [ ] getName ( ); void setName (char z[ ]); int getWeight ( ); void setWeight (int x); void bark( ); void growl( ); };

  34. Our Final CDog.cpp • #include <iostream.h> • #include <CDog.h> • // Constructor • CDog::CDog (int x, char y[ ]) { • rabid = false; • weight = x; • strcpy(name, y); • } • void CDog::eat ( ) { cout << name << “ is eating”; • } • void CDog::growl ( ) { • cout << “Grrrr”; • } Cdog.cpp Cdog.h bool CDog::getRabid ( ) { return rabid; } void CDog::setRabid (bool x) { rabid = x; } int CDog::getWeight ( ) { return weight; } void CDog::setWeight (int y) { weight = y; } char[ ] CDog::getName ( ) { return name; } void setName (char z[ ]) { name = z; }

  35. Summary of Class Concepts • A class is a generic description which may have many instances • When creating classes • Sketch the skeleton • Fill in the attributes • Make the constructor • Make the accessors/modifiers/miscellaneous • Classes go in separate files • The “.” and “->” operators tell the instances which method to run

  36. Inheritance and Objects

  37. Inheritance • Inheritance allows one class to take on the properties of another • Superclass-subclass relationship • Sometimes called parent-child relationship • Use the : <class name> to express this relationship • Subclass will “inherit” certain attributes and methods • Benefit: good design, reuse of code

  38. Class Hierarchy Mammal int weight giveBirth( ) Land-Mammal int numLegs Question: how many attributes does Dog have? Dog boolean rabid Chihuahua SheepDog

  39. Things to Note • Land-Mammal is a superclass to Dog, but a subclass to Mammal • Dog has three attributes • weight, numLegs and rabid • Two from inheritance, one it declared itself

  40. An Example(CDog.h – just review) #include <iostream.h> #include <string.h> class CDog { public: int weight; char name [255]; bool rabid; CDog(int x); void growl( ); };

  41. CDog.cpp(still review) • #include "CDog.h" • CDog::CDog(int x) { • weight = x; • rabid = false; • strcpy (name, "bob"); • cout << "A CDog was made" << endl; • } • void CDog::growl( ) { • cout << “Grrrr" << endl; • }

  42. Poodle: The evil subclass • Since a poodle is a type of CDog, how can we make class Poodle without re-writing a lot of code? • Does a poodle necessarily growl the same way a CDog growls?

  43. Class Poodle(both header and cpp file) #include "CDog.h“ class CPoodle:public CDog { public: CPoodle(int x); }; CPoodle::CPoodle(int x) : CDog (x){ cout << "A poodle was made" << endl; } Here’s where we inherit from CDog

  44. What’s going on here? CPoodle::CPoodle(int x) : CDog (x){ cout << "A poodle was made" << endl; } The super class’s constructoris always called*; in this case, we call the constructor which takes an int. Normal Constructor Stuff *Note: if you don’t explicitly call the super constructor, it looks fora no-arguments constructor in the super class (which we don’t have)

  45. What just happened? • We created a poodle that has all the attributes and all the methods that a CDog has • Problem: CPoodle* myPoodle = new CPoodle (3); myPoodle->growl ( ); // prints GRRRR • Poodles don’t go GRRRR, they “yip”

  46. Overriding methods • When you override a method, you give new functionality to the class • In this case, we need to override the growl method for the poodle • Simply redefine the method

  47. A Better Poodle(if there is such a thing) #include "CDog.h" class CPoodle:public CDog { public: CPoodle(int x); void growl(); }; CPoodle::CPoodle(int x) : CDog (x){ cout << "A poodle was made" << endl; } void CPoodle::growl( ) { cout << "Yip!" << endl; } Now when we tell the poodle to growl, it will print “Yip”

  48. Access Keywords: public, private(and protected) • We can restrict access to attributes and methods inside a class using public and private • public – means “everyone can see it” • private – means “only that class can see it”

  49. Bad Example(won’t compile) class X { private: int myInt; // Constructor… } void main ( ) { X* myX = new X ( ); myX->myInt = 42; // Not Allowed! }

  50. Working Example class X { public: int myInt; // Constructor… } void main ( ) { X* myX = new X ( ); myX->myInt = 42; // Allowed! }

More Related