Connecting Tech Pros Worldwide Help | Site Map

making a class

  #1  
Old April 22nd, 2007, 10:25 PM
Johs
Guest
 
Posts: n/a
I see a lot of different conventions on the net when it comes to making
a class. Currently I make a class in a single .cc file like:

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

class List {
private:
string name;
int id;

public:
// edit name
void setName(string newname);

// get name
string getName();

};

void List::setName(string str){
List::name = str;
}

string List::getName(){
return List::name;
}

int main(){
List mylist;
string newname = "Bo";
mylist.setName(newname);
string pp = mylist.getName();
cout << "sdfsdf \n" << pp << '\n';

return 0;
}


But is it better to leave implementation of the functions in another
file and keep the private/public sections in a .h file?
  #2  
Old April 22nd, 2007, 11:05 PM
bohemistanbul
Guest
 
Posts: n/a

re: making a class


On Apr 23, 12:25 am, Johs <a...@asd.comwrote:
Quote:
I see a lot of different conventions on the net when it comes to making
a class. Currently I make a class in a single .cc file like:
>
#include <string>
#include <iostream>
using namespace std;
>
class List {
private:
string name;
int id;
>
public:
// edit name
void setName(string newname);
>
// get name
string getName();
>
};
>
Up to this point, you can insert the above lines to a header file
namely List.h.
Quote:
void List::setName(string str){
List::name = str;
>
}
>
string List::getName(){
return List::name;
>
}
>
The function bodies should be located to the .cpp file namely
List.cpp

The main function should be located in main.cpp file. By this way, you
can easily insert or remove any precompiled files just only using
#include.
Quote:
int main(){
List mylist;
string newname = "Bo";
mylist.setName(newname);
string pp = mylist.getName();
cout << "sdfsdf \n" << pp << '\n';
>
return 0;
>
}
>
But is it better to leave implementation of the functions in another
file and keep the private/public sections in a .h file?

  #3  
Old April 22nd, 2007, 11:15 PM
Johs
Guest
 
Posts: n/a

re: making a class


bohemistanbul wrote:
Quote:
On Apr 23, 12:25 am, Johs <a...@asd.comwrote:
Quote:
>I see a lot of different conventions on the net when it comes to making
>a class. Currently I make a class in a single .cc file like:
>>
>#include <string>
>#include <iostream>
>using namespace std;
>>
>class List {
> private:
> string name;
> int id;
>>
> public:
> // edit name
> void setName(string newname);
>>
> // get name
> string getName();
>>
>};
>>
>
Up to this point, you can insert the above lines to a header file
namely List.h.
Ok, should there only be one class per header file or is it ok to
declare multiple classes per header file?

Another thing I have read that the extern keyword can be used. I have
omitted this so far and it works fine, is extern just an optional
keyword to make it more clearer if a variable is implemented somewhere else?
  #4  
Old April 22nd, 2007, 11:25 PM
Ian Collins
Guest
 
Posts: n/a

re: making a class


Johs wrote:
Quote:
bohemistanbul wrote:
>
Quote:
>>
>Up to this point, you can insert the above lines to a header file
>namely List.h.
>
Ok, should there only be one class per header file or is it ok to
declare multiple classes per header file?
>
There aren't any fixed rules, but one class per file keeps things short
and makes it easier to reuse the class.
Quote:
Another thing I have read that the extern keyword can be used. I have
omitted this so far and it works fine, is extern just an optional
keyword to make it more clearer if a variable is implemented somewhere
else?
The extern keyword is required for a variable declaration (extern int
foo;) where the variable is defined in a single compilation unit and
used elsewhere. It is optional for function prototypes.

--
Ian Collins.
  #5  
Old April 22nd, 2007, 11:45 PM
Johs
Guest
 
Posts: n/a

re: making a class


bohemistanbul wrote:
Quote:
On Apr 23, 12:25 am, Johs <a...@asd.comwrote:
Quote:
>I see a lot of different conventions on the net when it comes to making
>a class. Currently I make a class in a single .cc file like:
>>
>#include <string>
>#include <iostream>
>using namespace std;
>>
>class List {
> private:
> string name;
> int id;
>>
> public:
> // edit name
> void setName(string newname);
>>
> // get name
> string getName();
>>
>};
>>
>
Up to this point, you can insert the above lines to a header file
namely List.h.

Ok, should there only be one class per header file or is it ok to
declare multiple classes per header file?

Another thing I have read that the extern keyword can be used. I have
omitted this so far and it works fine, is extern just an optional
keyword to make it more clearer if a variable is implemented somewhere
else?
  #6  
Old April 23rd, 2007, 12:05 AM
Ian Collins
Guest
 
Posts: n/a

re: making a class


Johs wrote:
Quote:
>
Ok, should there only be one class per header file or is it ok to
declare multiple classes per header file?
>
You asked this half an hour ago.

--
Ian Collins.
  #7  
Old April 23rd, 2007, 12:35 AM
Gianni Mariani
Guest
 
Posts: n/a

re: making a class


Johs wrote:
....
Quote:
>
Ok, should there only be one class per header file or is it ok to
declare multiple classes per header file?
>
Another thing I have read that the extern keyword can be used. I have
omitted this so far and it works fine, is extern just an optional
keyword to make it more clearer if a variable is implemented somewhere
else?
I think one class per header would create far more complexity than there
needs to be.

One header per "interface" make better sense to me. e.g. if you're doing
a thread pool interface, then all the classes that are needed for the
thread pool should be in the same header. It would make little sense
though to have an interface for logging and another interface for fast
fourier transforms to be in the same header.

There used to be a desire to keep headers smaller because you could
eliminate compiling code that was never used, however, this is no longer
really an issue.

The real answer to your question is "whatever makes you and your clients
most efficient".

  #8  
Old April 24th, 2007, 04:25 AM
Juha Nieminen
Guest
 
Posts: n/a

re: making a class


Johs wrote:
Quote:
But is it better to leave implementation of the functions in another
file and keep the private/public sections in a .h file?
Many people have the misconception that a header file is a file
where you dump all the type and function declarations used by a
cpp file, regardless of whether those types/functions are used
anywhere else.

However, a header file declares the *public interface* of a module.
In other words, it declares types and functions which other modules
may need.

As with all public interfaces, they should contain as little
implementation details as possible. (Due to technical reasons the
private sections of classes have to be declared alongside their
public sections, but that's more or less unavoidable.)

If the implementation of a module (ie. the "cpp file") uses a
very small class, used exclusively by that module, there's often
no need to make that "public" by creating a header file for it.

Of course if that private class is bigger it will start cluttering
the cpp file if you keep it there. If that's the case, then you might
want to put it in a separate file and make a header file for it. It
all depends on how much clutter you avoid by doing that, I suppose.

(Sometimes, if the class is very small and all of its functions are
also very small, one option is to put the entire class, implementation
and all, in the header file, with all the functions declared inline.
This shouldn't be abused, though.)
  #9  
Old April 24th, 2007, 06:45 PM
BobR
Guest
 
Posts: n/a

re: making a class



Ian Collins <ian-news@hotmail.comwrote in message ...
Quote:
Johs wrote:
Quote:

Ok, should there only be one class per header file or is it ok to
declare multiple classes per header file?
You asked this half an hour ago.
- - Ian Collins.
"A.D.D." and programming don't mix well! <G>

--
Bob R
POVrookie


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class objects work like built-in types, but is it worth it? tonytech08 answers 55 October 30th, 2008 11:05 PM
Making class attributes non-case-sensitive? Rafe answers 11 October 16th, 2008 06:05 AM
Confused with the "virtual base class" concept mshetty answers 14 July 22nd, 2005 07:59 PM
multiple extensions to a class Humpty Dumpty answers 12 July 18th, 2005 01:39 PM