I have seen so many classes and inheritance programs but what I cant seem to know is when and why do u end up resorting in this statement #include "NameOfTheClass_h.h" and I guessed that is when u have separated the class from class main(), that sounds like making your program easy readable and easy relocatable, for a moment there I thought I Had the answer but that was not true

Ok now the problem..... I am using VS.Net 2005 and am so keen in knowing if the above statement is correct, do u open the new class project as same as the main class project and if so what about the space coz C++ class is alone big enough... ok I know this may sound stupid and naive but guys I really wana know this

Any help will be highly appreciated and please do not refer me to some other threads or sites

Recommended Answers

All 5 Replies

Edward can't speak for anyone else, but Ed uses header files to localize the declaration of a class and hiding of the definition. If you have a header, you don't have to repeat the class declaration for every single file you want to use the class in, and you also don't have to worry about multiple definition errors. For example, Edward will use a tiny class:

#include <iostream>
#include <string>

class Person {
  std::string _firstName;
  std::string _lastName;
public:
  Person(const std::string& first, const std::string& last);
  std::string ToString() const;
};

Person::Person(const std::string& first, const std::string& last)
  : _firstName(first), _lastName(last)
{}

std::string Person::ToString() const
{
  return _lastName + ", " + _firstName;
}

int main()
{
  Person me("Radical", "Edward");

  std::cout << me.ToString() << '\n';
}

Let's say that the Person class is useful and Edward's project contains multiple files. To use the Person class in more than one file, it has to be declared in each file. This is the declaration of the Person class:

#include <string>

class Person {
  std::string _firstName;
  std::string _lastName;
public:
  Person(const std::string& first, const std::string& last);
  std::string ToString() const;
};

The class itself is just a blueprint, but each file that needs to use the class also needs to know the public interface. If any children are defined in the file, it also needs to know the protected interface and any friends need to know the private interface. The actual method definitions aren't needed to use the class, so if you want to hide them you can move them into a separate file. Here are the method definitions:

Person::Person(const std::string& first, const std::string& last)
  : _firstName(first), _lastName(last)
{}

std::string Person::ToString() const
{
  return _lastName + ", " + _firstName;
}

Knowing how to split a class into declaration and definition means that you can put the declaration in its own file and save yourself the trouble of typing it over and over in each file you want to use the class. That file is called a header file:

#ifndef PERSON_CLASS
#define PERSON_CLASS

#include <string>

class Person {
  std::string _firstName;
  std::string _lastName;
public:
  Person(const std::string& first, const std::string& last);
  std::string ToString() const;
};

#endif

Only the required elements for using the class are present, and the #ifndef stuff makes up what are called header guards. You use header guards when you want to make sure that a header is only included once. The definition would go into a separate .cpp file and include the header for the declaration:

#include <string>
#include "person_class.h"

Person::Person(const std::string& first, const std::string& last)
  : _firstName(first), _lastName(last)
{}

std::string Person::ToString() const
{
  return _lastName + ", " + _firstName;
}

Edward names the two files the same so the relationship is obvious. Each of the files that use the Person class only need to include person_class.h and the whole project needs to link with person_class.cpp.

#include <iostream>
#include "person_class.h"

int main()
{
  Person me("Radical", "Edward");

  std::cout << me.ToString() << '\n';
}

Anyone who uses the class can look in the header to see the declaration but won't know how the interface is implemented unless the .cpp file is available too.

Does that help?

Thanx a lot Ed but can u help me to locate this....there is no way to have separate classes from the class main().... If you know JAVA you will know what Im talking about.. what I mean is... If for instance u have 20 classes, u write all the code(classes) in the same environment, that in the same cpp file??? that means u can have more than 1000 lines of codes

If you have a lot of classes that can be used in several different projects then put the classes in a library or DLL. That way you only have to complie the classes once. Then you tell the compiler to link the application program, that includes main(), with the library. One way to do that is use the #pragma directive

// main.cpp
#include <string>
// other includes here

// force the compiler to link with your library
#pragma comment(lib,"mylib.lib")

>> If for instance u have 20 classes, u write all the code(classes) in the same environment, that in the same cpp file???

No. Each class can be in its own *.cpp file, and all the *.cpp files can be in the same project. A project can contain as many *.cpp files as you want. And in Visual Studio projects and contain other projects.

commented: U see what I was telling u +1

> there is no way to have separate classes from the class main()....
There isn't a class that contains the main function like in Java. main() is a global function and you can have it in a separate file if you want. Most of Edward's projects have a main.cpp or program.cpp that contain the main function and other program-wide utilities. Ed's general guideline is to have a separate file for each class or collection of related functions. A C++ project is built like so:

  1. Compile each .cpp file separately
  2. Link all of the compiled .cpp files together

As long as you use headers or equivalent declarations so that each file can compile, the linking step is gravy. An IDE will even do it for you transparently. :)

> If you know JAVA you will know what Im talking about..
Ed has no trouble organizing classes in Java across multiple files. Have you worked with packages much? The concept is similar even if the details aren't.

> If for instance u have 20 classes, u write all the code(classes) in the same environment, that in the same cpp file???
You can if you want, but 20 classes all in one file would confuse dumb people like Edward. ;)

commented: Ur such darling, thanx hey +1

Yes!!!!! #pragma Ancient Melody, thats the word I was looking for....Thanx a lot guyz(Edward)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.