473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

making a class

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?
Apr 22 '07 #1
8 1739
On Apr 23, 12:25 am, Johs <a...@asd.comwrote:
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.
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.
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?

Apr 22 '07 #2
bohemistanbul wrote:
On Apr 23, 12:25 am, Johs <a...@asd.comwrote:
>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?
Apr 22 '07 #3
Johs wrote:
bohemistanbul wrote:
>>
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.
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.
Apr 22 '07 #4
bohemistanbul wrote:
On Apr 23, 12:25 am, Johs <a...@asd.comwrote:
>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?
Apr 22 '07 #5
Johs wrote:
>
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.
Apr 22 '07 #6
Johs wrote:
....
>
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".

Apr 22 '07 #7
Johs wrote:
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.)
Apr 24 '07 #8

Ian Collins <ia******@hotmail.comwrote in message ...
Johs wrote:

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
Apr 24 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Lars-Erik Aabech | last post by:
Hi! I've been looking around for an attribute that tells the intellitip thingy to skip some field or operation. Is there any? IE. a ListControl object has the DataSource property, but not all of...
10
by: Jeff Grills | last post by:
I am an experienced C++ programmer with over 12 years of development, and I think I know C++ quite well. I'm changing jobs at the moment, and I have about a month between leaving my last job and...
3
by: Peteroid | last post by:
Is it possible to make a public parent class method unavailable (i.e., generate an error at compile time) to a particular child class? For example, say a parent class has a public method Add( )....
3
by: Jarod_24 | last post by:
Throw new Excption ("bla, bla, bla") .... Catch E as Exception If E.Message.Equals("bla, bla, bla") Then ... Mostly i just use the standard "Exception"-object whenever i throw an exception,...
90
by: Ben Finney | last post by:
Howdy all, How can a (user-defined) class ensure that its instances are immutable, like an int or a tuple, without inheriting from those types? What caveats should be observed in making...
34
by: Asfand Yar Qazi | last post by:
Hi, I'm creating a library where several classes are intertwined rather tightly. I'm thinking of making them all use pimpls, so that these circular dependancies can be avoided easily, and I'm...
5
by: ffrugone | last post by:
My scenario involves two classes and a database. I have the classes "Broom" and "Closet". I want to use a static method from the "Closet" class to search the database for a matching "Broom". If...
12
by: Premal | last post by:
Hi, I tried to make delete operator private for my class. Strangely it is giving me error if I compile that code in VC++.NET. But it compiles successfully on VC++6.o. Can anybody give me inputs...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
11
by: Rafe | last post by:
Hi, I'm working within an application (making a lot of wrappers), but the application is not case sensitive. For example, Typing obj.name, obj.Name, or even object.naMe is all fine (as far as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.