Jump to content

Problem with classes in Visual Studio 2010 pro C++

- - - - -

  • Please log in to reply
8 replies to this topic

#1
Braydon Vaughan

Braydon Vaughan

    Newbie

  • Members
  • PipPip
  • 15 posts
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?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Braydon Vaughan

Braydon Vaughan

    Newbie

  • Members
  • PipPip
  • 15 posts

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
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
Here is how to define a function

#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
Braydon Vaughan

Braydon Vaughan

    Newbie

  • Members
  • PipPip
  • 15 posts
Thank you fayyazlodhi My weight for the robot now works and with the help of another person I understand why.

#6
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts
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.


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
Braydon Vaughan

Braydon Vaughan

    Newbie

  • Members
  • PipPip
  • 15 posts
How come you put double before everything? Was it a necessary step?

#8
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts
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.
I think i'm able to write a code for printing "Hello, World!". Proud of that!

#9
Braydon Vaughan

Braydon Vaughan

    Newbie

  • Members
  • PipPip
  • 15 posts
Thank you for your help also.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users