I started learning C++ about a week and a half ago and just started getting into functions and classes.
I use visual studio as my IDE and wrote this class:
#include <iostream>
class Robot
{
public:
int Weight();
unsigned int Height();
unsigned int Power();
void attack();
void run();
};
int main();
Robot Hornet;
Hornet.Weight = 200;
Hornet.Height = 20;
Hornet.Power = 200;
Hornet.attack();
Hornet.run();
Not having much experience in C++ coding I have no idea what is wrong. It will not compile correctly. There is something wrong with both the first section and second section of code. Could anyone help me try to figure out this problem with classes?
Problem with classes in Visual Studio 2010 pro C++
Started by Braydon Vaughan, Aug 04 2011 07:03 AM
8 replies to this topic
#1
Posted 04 August 2011 - 07:03 AM
|
|
|
#2
Posted 04 August 2011 - 07:53 AM
You have declared that Robot has several methods (Weight, Height, etc), but not defined them. In addition, your main method does not have braces around the code, and you are attempting to treat methods like properties.
#3
Posted 04 August 2011 - 08:09 AM
WingedPanther said:
You have declared that Robot has several methods (Weight, Height, etc), but not defined them. In addition, your main method does not have braces around the code, and you are attempting to treat methods like properties.
How would i define them? Set them equal to a number?
And also what do you mean treating them like properties?
And thank you, I added in the brackets.
#4
Posted 04 August 2011 - 09:55 AM
Here is how to define a function
Defining a function is different from defining a variable. Function is code. Variable is data. Usually functions execute some code on data.
Regarding the use of Property, it is a special feature of C# in which a private variable is directly accessible if required functions are defined much like you are assigning a variable i.e.
#include <iostream>
class Robot
{
private:
int weight_val;
public:
int Weight(int v)
{
if(v > 0 && v < 1000)
{
weight_val = v;
}
else weight_val = -1;
return weight_val;
}
};
int main()
{
Robot Hornet;
Hornet.Weight(200);
return 0;
}
Defining a function is different from defining a variable. Function is code. Variable is data. Usually functions execute some code on data.
Regarding the use of Property, it is a special feature of C# in which a private variable is directly accessible if required functions are defined much like you are assigning a variable i.e.
Object.Field = 4; // This is a property vs Object.Function(4); // This is a normal function which takes an integer and does something with it internally
Edited by fayyazlodhi, 04 August 2011 - 09:59 AM.
formatting
Today is the first day of the rest of my life
#5
Posted 04 August 2011 - 11:47 AM
Thank you fayyazlodhi My weight for the robot now works and with the help of another person I understand why.
#6
Posted 04 August 2011 - 11:55 AM
You have to take another class of classes in C++:thumbup1:.
Let me try,
Through classes we can declare and define user-defined data types that can be more realistic. If you go a step back into user-defined data types, probably you will find out Structures which are unsecure compared to Classes and less realistic. In Class we can put the data members under the public: so that we will have a direct access into it. Making them secure we get into private: and protected:. If we put the data members in private: then we have to access them through functions called member functions and also with friend functions. Member functions are those which are present in the same class and are under public:, so if we want to access a private data member then we need a member function or a friend funtion.
In your code you only have the member funtions and no data members. Also as i can see there are no function definitions also. If you want your robot alive then get some variables for it.:rules:
By the time i guess your robot needs Weight, Height & Power so it will cost three variables.
Define the member functions, you can define inside the class and outside aswell. I will be out :)
So at this stage we our class is ready. Let's try it out.
Hope it Helped!
Let me try,
Through classes we can declare and define user-defined data types that can be more realistic. If you go a step back into user-defined data types, probably you will find out Structures which are unsecure compared to Classes and less realistic. In Class we can put the data members under the public: so that we will have a direct access into it. Making them secure we get into private: and protected:. If we put the data members in private: then we have to access them through functions called member functions and also with friend functions. Member functions are those which are present in the same class and are under public:, so if we want to access a private data member then we need a member function or a friend funtion.
In your code you only have the member funtions and no data members. Also as i can see there are no function definitions also. If you want your robot alive then get some variables for it.:rules:
By the time i guess your robot needs Weight, Height & Power so it will cost three variables.
class Robot
{
/.../
private:
double weight;
double height;
double power;
};
Define the member functions, you can define inside the class and outside aswell. I will be out :)
void Robot::Weight(double w) {
[INDENT]weight = w;[/INDENT]
}
void Robot::Height(double h) {
[INDENT]height = h;[/INDENT]
}
void Robot::Power(double p) {
[INDENT]power = p;[/INDENT]
}
So at this stage we our class is ready. Let's try it out.
#include<iostream>
class Robot
{
public:
[INDENT]
Robot(double, double, double); // Default Constructor
void Weight(double);
void Height(double);
void Power(double);
[/INDENT]
private:
[INDENT]
double weight;
double height;
double power;
[/INDENT]
};
void Robot::Weight(double w) {
[INDENT]weight = w;[/INDENT]
}
void Robot::Height(double h) {
[INDENT]height = h;[/INDENT]
}
void Robot::Power(double p) {
[INDENT]power = p;[/INDENT]
}
int main();
[INDENT]
Robot Hornet;
Hornet.Weight(200);
Hornet.Height(20);
Hornet.Power(200);
[/INDENT]
return 0;
}
Hope it Helped!
I think i'm able to write a code for printing "Hello, World!". Proud of that!
#7
Posted 04 August 2011 - 12:02 PM
How come you put double before everything? Was it a necessary step?
#8
Posted 04 August 2011 - 12:18 PM
I guess yup! :P
Double is a data type. Won't you use a data type for a variable to store value? Porbably you have heard about float data type. In C++ double is used instead.
Double is a data type. Won't you use a data type for a variable to store value? Porbably you have heard about float data type. In C++ double is used instead.
I think i'm able to write a code for printing "Hello, World!". Proud of that!
#9
Posted 04 August 2011 - 12:58 PM
Thank you for your help also.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









