RUSES
Would you like to react to this message? Create an account in a few clicks or log in to continue.
May 2024
MonTueWedThuFriSatSun
  12345
6789101112
13141516171819
20212223242526
2728293031  

Calendar Calendar


Classes and objects

4 posters

Go down

Classes and objects Empty Classes and objects

Post  pidgeonsauce Sat Mar 17, 2012 3:41 am

Could someone please explain how you use classes, i understand how you create classes, i just cant get my code to use them and am unsure where i define variables and functions of the class

pidgeonsauce

Posts : 1
Join date : 2012-03-16

Back to top Go down

Classes and objects Empty Re: Classes and objects

Post  Alex Sat Mar 17, 2012 3:51 am

In general:

Code:
class Class_Name
{
   private:
   int var1;
   string var2;
   public:
   Class_Name(); //constructor
   void doSomething(int n); //random function
};

Class_Name::Class_Name()
{
   var1 = 4;
   var2 = "hi";
}

void Class_Name::doSomething(int n)
{
   var1 += n;
}

Extra info
http://www.cplusplus.com/doc/tutorial/classes/
http://www.cprogramming.com/tutorial/lesson12.html
Alex
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

Classes and objects Empty Re: Classes and objects

Post  ElliottH Sat Mar 17, 2012 6:40 am

Can be hard to explain, best way is to follow the examples like in the links Alex gave, and actually write them out yourself. When it finally clicks you can really start to see why C++ is so popular.
ElliottH
ElliottH
Admin

Posts : 16
Join date : 2012-02-23

Back to top Go down

Classes and objects Empty Re: Classes and objects

Post  Alex Sat Mar 17, 2012 8:18 am

Alex's Tutorial

Let's say we have the cat below, surround by his 'abilities'. i.e. he can meow, hiss or bite another cat. He also has a name:

Classes and objects Tibs10

This can be modelled as

Code:
class Cat
{
   private:
   string Name;

   public:
   void Meow();
   void Hiss();
   void Bite(Cat catThatIsBitten);

   void SetName(string name);
   string GetName() const;
};

Defining the functions below:

Code:
void Cat::Meow()
{
   cout << "Meow!" << endl;
}

void Cat::Hiss()
{
   cout << "Hiss!" << endl;
}

void Cat::Bite(Cat catThatIsBitten)
{
   cout << catThatIsBitten.GetName() << " was bitten by " << this->GetName() << endl;
   //i.e. Tibbles was bitten by Snowy
}

void Cat::SetName(string name)
{
   this->Name = name;
}

string Cat::GetName() const
{
   return this->Name;
}

Using this class:

Code:
int main()
{
   Cat Tibbles;
   Tibbles.SetName("Tibbles");

   Cat Snowy;
   Snowy.SetName("Snowy");

   Tibbles.Meow();
   Snowy.Hiss();

   Snowy.Bite(Tibbles);

   cin.get();

   return 0;
}


Last edited by Alex on Sat Mar 17, 2012 8:27 am; edited 1 time in total
Alex
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

Classes and objects Empty Re: Classes and objects

Post  Alex Sat Mar 17, 2012 8:26 am

Constructors and Destructors

In the above code, it's quite annoying calling two lines to set the cat's name:

Code:
Cat Snowy;
Snowy.SetName("Snowy");

Wouldn't it be easier to make that only take up one line of code? (yes)

This is where constructors come in handy. Constructors are a function called when the class is created.

A constructor's function name is the same name as the class. So since the class name is Cat, the constructor will be Cat.

Constructors can take arguments and they don't return a value.

Below I've modified the class to add a constructor which takes a string parameter, the string passed into the constructor will be the name of the cat.

Code:
class Cat
{
   private:
   string Name;

   public:
   //constructor
   Cat(string Name);

   void Meow();
   void Hiss();
   void Bite(Cat catThatIsBitten);

   void SetName(string name);
   string GetName() const;
};

Now we need to define the constructor:

Code:
Cat::Cat(string name)
{
   this->Name = name;
}

So the main function can now be modified:

Code:
int main()
{
   Cat Tibbles("Tibbles");

   Cat Snowy("Snowy");

   Tibbles.Meow();
   Snowy.Hiss();

   Snowy.Bite(Tibbles);

   cin.get();

   return 0;
}
Alex
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

Classes and objects Empty Re: Classes and objects

Post  Alex Sat Mar 17, 2012 8:34 am

A destructor is the opposite of a constructor, (although you must manually call it) when you want to delete an object

Destructors can take no parameter and they cannot return a value

It is defined like the constructor except it starts with a squiggle ~
i.e.

Constructor = Cat()
Destructor = ~Cat()

Though the example I'm about to do is pointless, it will help explain a destructor.

Let's say when we want to delete a Cat object, we want to set its name to "" - a blank string

First we add the destructor to the Cat class:

Code:
class Cat
{
   private:
   string Name;

   public:
   //constructor
   Cat(string Name);
   //destructor
   ~Cat();

   void Meow();
   void Hiss();
   void Bite(Cat catThatIsBitten);

   void SetName(string name);
   string GetName() const;
};

Now we must declare the destructor:

Code:
Cat::~Cat()
{
   this->Name = "";
}

Thus the destructors can be added to the main function:

Code:
int main()
{
   Cat Tibbles("Tibbles");

   Cat Snowy("Snowy");

   Tibbles.Meow();
   Snowy.Hiss();

   Snowy.Bite(Tibbles);

   cin.get();

   Snowy.~Cat();
   Tibbles.~Cat();

   return 0;
}

The above example is pointless. The main use of destructors (in my opinion) is to delete any dynamic memory you may have allocated in the lifespan of the class, a quick example to show this:

Code:
class Foo
{
   int *Arr; //integer array
   public:
   Foo();
   ~Foo();
};

Foo::Foo()
{
   //allocate memory
   Arr = new int[100];
}

Foo::~Foo()
{
   //delete memory
   delete[] Arr;
}

int main()
{
   Foo f;
   //f has an array of 100 integers allocated to it

   //do stuff with f
   
   //clear up the dynamic memory before we return (good practice)
   f.~Foo();

   return 0;
}
Alex
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

Classes and objects Empty Re: Classes and objects

Post  Alex Sat Mar 17, 2012 8:37 am

If anyone's interested + I can be bothered I'll carry on with

  • Constructor Overloading
  • Inheritance
  • Operator Overloading
Alex
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

Classes and objects Empty Re: Classes and objects

Post  John Sat Mar 17, 2012 9:02 am

Very Happy thanks thats really good!!!!
John
John
Admin

Posts : 36
Join date : 2012-02-23
Age : 32

https://ruses.forumotion.co.uk

Back to top Go down

Classes and objects Empty Re: Classes and objects

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum