need help in my code

+3 danial saufi · November 16, 2014
sorry if its not allowed to ask here or anything. i jsut signed up today. anyway i got this problem with this c++ code. it doesnt run.
#include <iostream>
#include <string>
using namespace std;

class theClass(){
    string x;
public:
    function getName(){

        cout << "What is your name?" << endl;
        cin >> x >>  endl;
        return x;
    }
    function sayName(){
        cout << "hello" << x << endl;
    }
}
int main(){

    theClass dc;
    dc.getName();
    dc.sayName();


}
please dont laugh cause im new in c++. just got about 28 of buckys c++ vids.
and lastly if we put it as private.. how do we access it? the one where bucky used private and at the top he used public was abit confusing

Post a Reply

Replies

Oldest  Newest  Rating
-1 Homer Simpson · November 17, 2014
All global variables should be declared private. It protects your data. But what does that mean, well lets look at an example:

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

using namespace std;

class Dog{
public:
int dogAge;
};

int main()
{
    Dog fluffy;
    cout << "How old is your dog: ";
    cin >> fluffy.dogAge;

    cout << "Fluffy is " << fluffy.dogAge << " years old." << endl;
    
    return 0;
}


We have a dog named fluffy and we're letting the user enter in the dogs age. Cool, but what if they put something ridiculous like -17. We can prevent this by setting our dogAge variable private. 

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

using namespace std;

class Dog{
private:
    int dogAge;
};

int main()
{
    Dog fluffy;
    cout << "How old is your dog: ";
    cin >> fluffy.dogAge;

    cout << "Fluffy is " << fluffy.dogAge << " years old." << endl;

    return 0;
}

I tried to run this in CodeBlocks and it has errors. This is because the variable is private and cant be accessed directly outside of the class. So what we can do is set up some accessor(get) and mutator(set) functions like so


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

using namespace std;

class Dog{
private:
    int dogAge;

public:
    void setDogAge(int age)
    {
        if(age > 0 && age < 100)
            dogAge = age;
        else
            dogAge = 0;
    }

    int getDogAge()
    {
        return dogAge;
    }
};

int main()
{
    Dog fluffy;
    int userInput;
    cout << "How old is your dog: ";
    cin >> userInput;
    fluffy.setDogAge(userInput);

    cout << "Fluffy is " << fluffy.getDogAge() << " years old." << endl;

    return 0;
}

In the setDogAge function it says that if the user enters any number below 0 or over 100. Set the dogs age to 0. Long story short, private variables protect your data from stupidity. :] I hope this helped.
0 danial saufi · November 17, 2014
Ohhhhhhh... function is not a c++ word? I think i messed up with other languages. Tyvm and can you explain to me about the public or private. I saw buckys vid but i didnt quite understand it.
+1 Jason Parthenis · November 16, 2014
I am not good with explainations but i can give you working code...

#include <iostream>
#include <string>
using namespace std;

class theClass {
    string x;

    public:
        string getName(){
            cout << "What is your name?" << endl;
            cin >> x;
            return x;
        }

        void sayName(){
            cout << "hello " << x << endl;
        }
};

int main(){
    theClass dc;
    dc.getName();
    dc.sayName();
    system("PAUSE");
}
  • 1

C++

107,164 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator