Apr 5, 2016 04:55 PM
(This post was last modified: Apr 5, 2016 05:11 PM by x41N.)
Most of this code is comments, because I am learning C++...
But I cannot for the life of me figure out why, this pointer object can't call this method...
If someone could tell me why I would appreciate it...
This is the method I am trying to call...
Also I know some of my wording in my comments are incorrect, please don't flame me for them. I came for help.
Thank you.
Added a pastebin if you prefer to view it from there...
http://pastebin.com/3J52nL6S
EDIT: IM AN IDIOT AND IT WAS OUTPUTTING THE ENTIRE TIME... LOL
But I cannot for the life of me figure out why, this pointer object can't call this method...
If someone could tell me why I would appreciate it...
This is the method I am trying to call...
PHP Code
<?php
pCat->speak();
Also I know some of my wording in my comments are incorrect, please don't flame me for them. I came for help.
Thank you.
PHP Code
<?php
#include <iostream>
#include <string>
using namespace std;
/*
Creating a Animal class with:
Private method instance variables
Public Methods
Constructor
Copy Constructor
Destructor
*/
class Animals
{
private:
string name;
public:
//Constructor Here
Animals() { cout << "Animals created" << endl; }
//Copy Constructor Here
Animals(const Animals &reference) :name(reference.name) { cout << "Animal created by a copy constructor." << endl; }
//Destructor Here
~Animals(){cout << "Destructor Called" << endl;}
//Methods
void setName(string name) { this->name = name; }
void speak() const { cout << "This animal is called: " << name << endl; }
};
/*
PICTURES:
https://i.imgur.com/2c9dn5T.png
*/
int main()
{
/*
Okay I am going to instantiate an object from our class Animals. You can see I have called it cat
and I have called some class methods, on our cat object and set it some states.
When we instantiate objects like this they deallocate from the memory themselves when they reach the end of their
variable scope, in this example when we reach the end of main.
I am now going to instantiate a pointer object using the 'new' keyword/operator.
*/
Animals cat;
cat.setName("Robert");
cat.speak();
/*
Here you can see I have instantiated a new object called 'pCat' we can also assign states using our class
methods to this object also.
The new operator allocates memory that you can manage yourself, in this case to a single object called 'pCat'
The 'Animals()' is our constructor, if our constructor took parameters, we would put them inside these brackets.
*/
Animals *pCat = new Animals();
/*
pCat.setName("Robert");
pCat.speak();
Calling our methods like this is not going to work when using pointers, even if we dereference them...
This is because the '.' has a higher precedence then the '*' operator. So we have to use special syntax,
which is '->' you have seen this before with the 'this' pointer variable.
*/
pCat->setName("Gareth");
pCat->speak();
delete pCat;
return 0;
}
Added a pastebin if you prefer to view it from there...
http://pastebin.com/3J52nL6S
EDIT: IM AN IDIOT AND IT WAS OUTPUTTING THE ENTIRE TIME... LOL
0xH Alpha | NFT Enthusiast | Crypto Enthusiast

