970 likes | 1.1k Vues
This guide provides a comprehensive overview of C++ classes, covering essential terminology, file structure, and class design. Learn how to create classes, define attributes (data members), and implement behaviors (methods) for your objects. We dive into constructors, accessors, and modifiers while emphasizing the importance of file topology. Through practical examples, including a driver program to instantiate and control class objects, you will gain a solid understanding of object-oriented programming in C++. This tutorial is perfect for both beginners and those looking to refresh their C++ skills. ###
E N D
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! • 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)
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
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!
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)
A “real life” example • The CDog • Attributes (characteristics) • rabid or not rabid (bool) • weight (int or float) • name (char [ ]) • Behaviors • growl • eat
Step 1: The Skeleton • class CDog { • // attributes will go here – name, weight, rabid • // behaviors will go here – growl, eat • };
Step 2: The attributes(we’ll discuss public later) • class CDog { • public: • boolean rabid; • int weight; • char name[255]; • // Behaviors go here • };
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
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.
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
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
Step 1: Type in the Skeleton #include <iostream.h> void main ( ) { // Create your instances here! }
Step 2: Declare A CDog Pointer #include <iostream.h> void main ( ) { CDog* c1; } • // null means “dead” Memory null c1
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>);
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); • }
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); • }
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); • }
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”
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”
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”
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”
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”
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 • };
Miscellaneous Methods • Follow the pattern void CDog::eat ( ) { cout << name << “ is now eating” << endl; weight++; } void CDog::growl ( ) { cout << “Grrrr” << endl; }
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; } • };
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>
Using the “.” and “->” Operators #include <iostream.h> void main ( ) { CDog* c1; c1 = new CDog (14, “Bob”); CDog c2 (7, “Ethel”); c2.bark( ); c1->growl( ); }
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
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
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; }
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( ); };
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; }
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
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
Class Hierarchy Mammal int weight giveBirth( ) Land-Mammal int numLegs Question: how many attributes does Dog have? Dog boolean rabid Chihuahua SheepDog
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
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( ); };
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; • }
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?
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
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)
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”
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
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”
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”
Bad Example(won’t compile) class X { private: int myInt; // Constructor… } void main ( ) { X* myX = new X ( ); myX->myInt = 42; // Not Allowed! }
Working Example class X { public: int myInt; // Constructor… } void main ( ) { X* myX = new X ( ); myX->myInt = 42; // Allowed! }