Welcome
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials!

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>!

am i right track

C++ Stuff belonging in global scope pretty much

am i right track

Postby masterkey on Fri Oct 23, 2009 8:33 pm

Code: Select all
#include <iostream>
#include <windows.h>


using namespace std;


class wizard
{
public:
   //costructors\deconstructors
wizard();
~wizard();



//health
int gethealth(){return *health;}
void sethealth(int heal){  *health =  heal;}
int increase_health(){return ++*health ;}
int reduce_health(){return --*health;}
//strength
int get_strength(){return *strength;}
void set_strength(int power){*strength = power;}
int increase_strength(){return ++*strength;}
int reduce_strength(){return --*strength;}
//currency
int get_gold_coins(){return *gold_coins;}
void set_gold_coins(int amount){*gold_coins = amount;}
int increase_gold_coins(){return ++*gold_coins;}
int reduce_gold_coins(){return --*gold_coins;}
//stealth weapon
bool init_stealth_mode(){return *stealth_mode = true;}
bool check_stealth_mode(){return *stealth_mode;}
bool set_stealth_mode(){return *stealth_mode = false;}
//attacking
void attack(){cout <<"you are attacking...\n";}
void being_attacked(){cout << "you are being attacked....\n";}
// doors
bool check_door(){return *door;}
void open_door(){*door = true ;}
void close_door(){*door = false;}
// keys
bool check_key(){return *masterkey;}
void use_key(){*masterkey = true ;}
void put_away_key(){*masterkey = false;}
//transport
void ride_ninja_bike()
{cout <<"\nriding....\n";
Sleep(5000);
cout << "you have arrived\n";}
//fly



//variables
private:
int *health;
int *strength;
int *gold_coins;
bool *stealth_mode;
bool *door;
bool *masterkey;
bool *ninja_bike;
};
//constructor
wizard::wizard()
{
    health = new int;
    strength = new int;
     gold_coins = new int;
     stealth_mode = new bool;          //put timer with this(reminder)
    door = new bool;
    masterkey =new bool;
     ninja_bike = new bool;
}

//destructor
wizard::~wizard()
{
delete health;
delete strength;
delete gold_coins;
delete stealth_mode;
delete door;
delete masterkey;
delete ninja_bike;
}


int main()
{
wizard *darth_vader = new wizard;    // i aint started on the main bit yet cos
wizard *merlin = new wizard;         // i wanna get all my varibles
merlin->sethealth(9);                // and functions sorted
merlin->set_strength(9);
merlin->set_gold_coins(9);
darth_vader->sethealth(3);
darth_vader->set_strength(3);
darth_vader->set_gold_coins(9);
cout <<darth_vader->gethealth()<< "\n";
darth_vader->ride_ninja_bike();



delete darth_vader;
delete merlin;
system("pause");
return 0;
}
masterkey
 
Posts: 19
Joined: Tue Sep 22, 2009 5:31 pm

Re: am i right track

Postby noobgrammer on Fri Oct 23, 2009 9:38 pm

here lets get you on the right rails

wizard.h
Code: Select all
#ifndef WIZARD_H
#define WIZARD_H

#include <iostream>
    #include <windows.h>

class wizard
    {
    public:
       //costructors\deconstructors
    wizard(int , int , int , bool , bool , bool , bool);
   ~wizard(){}



    //health
    int gethealth(){return health;}
    void sethealth(int heal){  health =  heal;}//this isn't adding to health just reassigning it
    int increase_health(){return ++health ;}
    int reduce_health(){return --health;}
    //strength
    int get_strength(){return strength;}
    void set_strength(int power){strength = power;}//this isn't adding to strength just reassigning it
    int increase_strength(){return ++strength;}
    int reduce_strength(){return --strength;}
    //currency
    int get_gold_coins(){return gold_coins;}
    void set_gold_coins(int amount){gold_coins = amount;}
    int increase_gold_coins(){return ++gold_coins;}
    int reduce_gold_coins(){return --gold_coins;}
    //stealth weapon
    bool init_stealth_mode(){return stealth_mode = true;}
    bool check_stealth_mode(){return stealth_mode;}
    bool set_stealth_mode(){return stealth_mode = false;}
    //attacking
   void attack(){std::cout <<"you are attacking...\n";}
   void being_attacked(){std::cout << "you are being attacked....\n";}
    // doors
    bool check_door(){return door;}
    void open_door(){door = true ;}
    void close_door(){door = false;}
    // keys
    bool check_key(){return masterkey;}
    void use_key(){masterkey = true ;}
    void put_away_key(){masterkey = false;}
    //transport
    void ride_ninja_bike()
   {std::cout <<"\nriding....\n";
    Sleep(5000);
   std::cout << "you have arrived\n";}
    //fly



    //variables
    private:
    int health;
    int strength;
    int gold_coins;
    bool stealth_mode;
    bool door;
    bool masterkey;
    bool ninja_bike;
    };

#endif

wizard.cpp
Code: Select all
#include "wizard.h"

wizard::wizard(int _health, int _strength, int _gold_coins, bool _stealth_mode, bool _door, bool _masterkey, bool _ninja_bike) :
   
        health(_health),
        strength(_strength),
         gold_coins(_gold_coins),
         stealth_mode(_stealth_mode),          //put timer with this(reminder)
        door(_door),
        masterkey(_masterkey),
         ninja_bike(_ninja_bike)
   {}

main.cpp
Code: Select all
   
#include "wizard.h"


    int main()
    {
    wizard darth_vader(1,2,3,true,false,true,false);    // i aint started on the main bit yet cos
    wizard merlin(1,2,3,true,false,true,false);         // i wanna get all my varibles
    merlin.sethealth(9);                // and functions sorted
    merlin.set_strength(9);
    merlin.set_gold_coins(9);
    darth_vader.sethealth(3);
    darth_vader.set_strength(3);
    darth_vader.set_gold_coins(9);
   std::cout <<darth_vader.gethealth()<< "\n";
    darth_vader.ride_ninja_bike();



   
    system("pause");
    return 0;
    }


I'm a little tired so I will be brief I've already told you how to do it right and did it so you will have a example to look at
I see a couple problems I believe I will show one you will have to hunt the others down

Code: Select all
void sethealth(int heal){  health =  heal;}//this isn't adding to health just reassigning it


I'm tired so I don't know if there are more mistakes but there is a start on the right path
I need a compiler with a can of RAID built into it

I added a msn messenger just for programming feel free to email or add yourself to it
User avatar
noobgrammer
 
Posts: 170
Joined: Tue Aug 25, 2009 10:44 am
Location: Orlando


Return to General

Who is online

Users browsing this forum: No registered users and 0 guests