Archived

This topic is now archived and is closed to further replies.

death_jester

bad code?

Recommended Posts

It's not "int main()". It should be like this:

#include <iostream>

void main() {

class Dog()
{
public:
unsigned int itsAge;
unsigned int itsWeight;
Bark();
};
}

Edited by - BioSquirrel on January 12, 2002 11:26:15 AM

Share this post


Link to post
Share on other sites

First you have to put a semicolomn ";" after the last brace
in the class declararion.

Also do not forget to put "()" after "class Dog" because
then the compiler thinks that "Dog" is a function.

Then I would put the class declarartion in a header file.
After that I would put the definition og it´s class members in
its own source file.
After that include the header file in your project.

Class would now look like this and compile fine.


class Dog
{
public:
unsigned int itsAge;
unsigned int itsWeight;
Bark();
};



Share this post


Link to post
Share on other sites
// this is main.cpp

#include <iostream>
#include "Dog.h";
using namespace std;

int main()
{
Dog mydog;
mydog.Bark();
...
...
...
}


// now comes Dog.h

class Dog
{
public:
unsigned int itsAge;
unsigned int itsWeight;
Bark();
};

// and this is Dog.cpp

Dog::Dog()
{
itsAge = 5;
itsWeight = 10;
}

Dog::Bark()
{
cout << "VOFF\n";
}



Hope it helps, not tried though.

Share this post


Link to post
Share on other sites
quote:
Original post by BioSquirrel
It''s not "int main()". It should be like this:

void main()

Declaring main with a return type of void is illegal as of the C and C++ standards. Shame on you!



Once there was a time when all people believed in God and the church ruled. This time is called the Dark Ages.

Share this post


Link to post
Share on other sites
Hmmm...that''s odd. I remember reading a TON of C++ tutorials that said "void main()"...although those tutorials might not have complied with the standard.

-----------------------------
"C++ isn''''t hard, you fool!"
Famous Last Words
-----------------------------

Share this post


Link to post
Share on other sites
dude, almost everything about it is wrong.
    

//----------- main.cpp ---------------//

#include "dog.h"

#include <iostream.h>


int main(void)
{
CDog myDog;
cout<<myDog.Bark()<<endl;
return 0;
}

//----------- dog.h ------------------//

#ifndef _DOG_H

#define _DOG_H

#ifndef _STRING_H

#include <string.h>

#endif


class CDog{
public:
string Bark(void);
unsigned int GetAge(void);
unsigned int GetWeight(void);
private:
unsigned int UIage,UIweight;
};

#endif


//-------------- dog.cpp -------------//

#ifndef _DOG_H

#include "dog.h"

#endif

#ifndef _STRING_H

#include <string.h>

#endif


CDog::CDog(void)
{
UIAge=5;
UIWeight=30;
}

inline string CDog::Bark(void){
return "Woof!";
}

inline unsigned int CDog::GetAge(void){
return UIAge;
}

inline unsigned int CDog::GetWeight(void){
return UIWeight;
}

you missed all the naming conventions and your code was implying that your header would include possibly non-portable code (the input/ouptut system). iostream.h is very portable, but what if he was using some other system? Heck, I've probably missed some naming conventions, that was all I could remember off the top of my head.

One tip, never include a file that you don't need. This is a major code file size optimization.

Edited by - capn_midnight on January 12, 2002 8:54:16 PM

Share this post


Link to post
Share on other sites