Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Why is this pointer object unable to read the string from my speak method?

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...
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


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by Equivalent_Jaguar198Jun 13, 2022 02:20 PM 4 188 Jun 13, 2022 02:20 PM
Last Post: Equivalent_Jaguar198
[C] by MordiciaNov 15, 2019 07:21 PM 3 211 Nov 15, 2019 07:21 PM
Last Post: Mordicia
[C] by YouthInAsiaNov 13, 2019 04:21 AM 9 373 Nov 13, 2019 04:21 AM
Last Post: YouthInAsia