-
help in object
i have this program and i cant compile it
Code:
#include<stdio.h>
#include<iostream>
#include<conio.h>
class dog
{
int age, weight;
char color;
public:
void setvalues(int, int, char);
};
void dog::setvalues(int age, int weight, char color)
{
this -> age = age; // using this-> will explicily refer to the class member
weight -> = weight;
color -> = color;
};
main()
{
dog fido;
fido.setvalues(12,55,"gray");
getch();
}
-
At first a lot of sintax errors!!
Code:
#include<stdio.h>
#include<iostream>
#include<conio.h>
class dog
{
int age, weight;
char color;
public:
void setvalues(int, int, char);
};
void dog::setvalues(int age, int weight, char color)
{
this -> age = age; // using this-> will explicily refer to the class member
?? weight -> = weight;
this->weight = weight;
// see before color -> = color;
};
main()
{
dog fido;
fido.setvalues(12,55,"gray");
getch();
}
2) setvalues if for a char data and not for a *char data.
-
Code:
#include<iostream>
#include <string>
class dog
{
int m_age, m_weight;
std::string m_color;
public:
void setvalues(int age, int weight, const std::string& color);
};
void dog::setvalues(int age, int weight, const std::string& color)
{
m_age = age; // using this-> will explicily refer to the class member
m_weight = weight;
m_color = color;
}
int main()
{
dog fido;
fido.setvalues(12,55,"gray");
}
I have fixed the code to make it compile.
I will explain a few things. Others can fill in the rest.
char is a character, double quotes indicates a string. A string is of type std::string.
You don't need to use "this->" to refer to member. That is, if your members are of different names than your parameters.
weight -> makes no sense.
main must have a return type. That type shall be int.