A Couple of Questions

This is a discussion on A Couple of Questions within the C++ Programming forums, part of the General Programming Boards category; Hello, it's toonlover here again, I have a couple of questions that I'm not quite sure how to use them. ...

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103

    A Couple of Questions

    Hello, it's toonlover here again, I have a couple of questions that I'm not quite sure how to use them. Please , I'd sure would apriciate it:

    -> Why sometimes they use .cpp as a library and not .h
    -> What exactly does #pragma , #ifdef , #ifndef , and #undef do?
    -> How can you make your program change it's type of file, example, when finishing compliling, it creates an .exe, instead of .exe, how can I choose what type I want it to be instead of .exe. How?

    Thankyou in advance

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Why sometimes they use .cpp as a library and not .h
    By convention, .h files contain declarations and .cpp files contain definitions. Definitions can only appear once in the whole program. Declarations can be included once per file, but they can appear in many files.

    An #include statement causes the contents of the specified file to replace the include statement. Since you can structure a program like this:
    Code:
    #include<iostream>
    using namespace std;
    
    //declaration of class:
    class MyClass
    {
    public:
    	void display();
    };
    
    //Definition of member function:
    void MyClass::display()
    {
    	cout<<"greetings from MyClass"<<endl;
    }
    
    int main()
    {
    	MyClass obj;
    	obj.display();
    	
    	return 0;
    }
    you can also put this in a separate file and include it:
    Code:
    //declaration of class:
    class MyClass
    {
    public:
    	void display();
    };
    
    //Definition of member function:
    void MyClass::display()
    {
    	cout<<"greetings from MyClass"<<endl;
    }
    It doesn't matter if you name that file with a .h extension or a .cpp extension, the contents will replace the include statement. The problem with putting both the declarations and definitions in the same file goes back to the first rule I stated: definitions can only appear once in a program. However, you also have to introduce all the names in a file before using them, which means you may need to introduce MyClass at the top of more than one file if the name MyClass is used in more than one file. The way you introduce a name in a file is by including its declaration in the file. With the declarations and definitions in the same file, if you include that file more than once then the definitions will appear more than once.

    Therefore, you would normally split that section up into two parts: by convention the declaration would be in a .h file and the definitions in a .cpp file. Then, you would include the .h file in any file that used the name MyClass.


    What exactly does #pragma , #ifdef , #ifndef , and #undef do?
    They are all preprocessor directives, which are statements the compiler executes before compiling your program.

    #pragma doesn't do anything exactly. It is a command reserved for a compiler to do what it wants with it, so you have to check a compiler's specific documentation to know what it does.

    #ifdef and #ifndef are if statements that check to see if a name is 'defined'. When something is defined it exists, when something isn't defined or it is defined and subsequently undefined(#undef), it doesn't exist. That can be useful to prevent code duplication. Header files typically are surrounded by:
    Code:
    #ifndef SOME_NAME_H
    #def SOME_NAME_H
    
    //declarations
    
    #endif
    They are used to prevent including a header file more than once in the same file. When there are a lot of files in your program and you are including files that include other files, it can be almost impossible to work out whether you are including a header file more than once in a file. So with those "inclusion guards", you can prevent a header file from being included more than once in the same file. The first time the file is included, SOME_NAME_H won't exist so it won't be defined, and the preprocessor enters the if-block. The first statement inside the if-block is to define the name SOME_NAME_H, so now it exists. Then, the rest of the code is included in the file. If an attempt is made to include that file again in the same file, then the if condition will fail, and the preprocessor will skip the code in the if-block and end up including nothing.

    #define can be used to substitute things in your code. For instance:

    #define PI 3.142

    will replace the characters PI anywhere they appear in your program with 3.142. If at some point in your file, you don't want that substitution to occur anymore, you can undefine PI:

    #undefine PI

    Then no more substitutions for the characters PI will occur from that point on.

    How can you make your program change it's type of file, example, when finishing compiling, it creates an .exe, instead of .exe, how can I choose what type I want it to be instead of .exe. How?
    I don't think you can. It seems to me that the whole purpose of compiling a program is to create a .exe file that you can execute. Why would you want to compile a program you didn't want to execute?
    Last edited by 7stud; 12-02-2005 at 10:39 PM.

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    Well by changind the type of file my program came out with, I wanted it to change to .scr, to make it into a screensaver. Well, thankyou 7studl, for helping me with all this questions

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Most of the compilers I know understand the -o switch
    like
    Code:
    g++ main.cc -o screensaver.scr
    K

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Or you can rename the generated output file:

    windows: "ren output.exe output.scr"
    linux: "mv output output.scr"

    since .scr is exactly the same as .exe. In fact, that's how I recently wrote my own screen saver.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    Thankyou guy guys, I'll try those methods right now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple of Questions About Classes
    By bengreenwood in forum C++ Programming
    Replies: 3
    Last Post: 05-20-2009, 03:50 PM
  2. Couple of questions about XOR linked lists...
    By klawson88 in forum C Programming
    Replies: 5
    Last Post: 04-19-2009, 05:55 PM
  3. A couple questions
    By Flakster in forum C++ Programming
    Replies: 7
    Last Post: 08-09-2005, 02:22 PM
  4. Couple of simple directdraw questions.
    By Deo in forum Game Programming
    Replies: 3
    Last Post: 05-25-2005, 08:55 AM
  5. Studying for Final, Couple Questions...
    By stewade in forum C Programming
    Replies: 4
    Last Post: 05-10-2004, 06:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21