Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // More C++ Tutorial at BrandonHok.com
- #include <iostream>
- // Classes, constructors, memberwise assignment
- class FireBurning
- {
- public:
- // This is the default constructor, similar to a prototype with
- // return types. This sets up basic outline of an object.
- // By default, the FireBurning temperature will be 500 celsius and 100 meters long.
- // Constructors generally must have the same name as the class name
- FireBurning(int celsius = 500, int meters = 100);
- // This is the first function or action that you want your class to have/do
- void fireShot();
- private:
- // These are data variables that you want the class to have.
- // It is good practice to label private variables with '_' at the end of its name.
- int celsius_;
- int meters_;
- };
- // Class name :: Constructor (same name as class name)
- FireBurning::FireBurning(int c, int m)
- {
- // This allows to set/intializes our variables to our object. You will see below.
- celsius_ = c;
- meters_ = m;
- }
- // Class name :: member function
- void FireBurning::fireShot()
- {
- // This is your function. Design it the same as a function.
- std::cout << "How hot? Celsius = " << celsius_ << std::endl;
- std::cout << "How many meters? " << meters_ << std::endl;
- }
- int main()
- {
- // Declare your first object
- // Classname objectname
- FireBurning firstFire;
- // Declare your second object and set the arguments. Remember the constructor?
- FireBurning secondFire(300, 200);
- // Now we can command the objects with our member functions that we assigned it.
- // Test them out!!
- // Use the objectname . memberfunction ;
- // dot notation is the .
- firstFire.fireShot();
- secondFire.fireShot();
- std::cout << "Our next line of shots!\n";
- // What happens here? All the variables are transferred to the left object!
- // The "=" represents the default memberwise assignment
- secondFire = firstFire;
- secondFire.fireShot();
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement