how could I use my own class ?

This is a discussion on how could I use my own class ? within the C++ Programming forums, part of the General Programming Boards category; Code: /////////////////////////myclass.h #ifndef MYCLASS_H #define MYCLASS_H #include <iostream.h> class Example { Example( ); }; #endif ////////////////////////myclass.cpp #include "myclass.h" Example::Example() { ...

  1. #16
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Code:
    /////////////////////////myclass.h
    
    #ifndef MYCLASS_H
    #define MYCLASS_H
    
    #include <iostream.h>
    class Example
    {
        Example( );
    };
    
    #endif
    
    ////////////////////////myclass.cpp
    
    #include "myclass.h"
    
    Example::Example()
    {
       cout << "Woohoo!!" << endl;
    }
    
    ///////////////////////main.cpp
    
    #include "myclass.h"
    
    void main()
    {
         Example eg;
    }

  2. #17
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Well, that's a good illustration. No destructor (can't remember if the compiler defaults it if not included). But shouldn't your #include <iostream.h> statement be in the myclass.cpp file?
    Truth is a malleable commodity - Dick Cheney

  3. #18
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Post

    I am learning C++ no so long, so I do not know the method clearly.

    here is a class I created, how could divide them into a header file and a definition(cpp) file ? and is there any limitation of the place they stays ?

    class mankind
    {
    public:
    mankind(string,int);
    private:
    string name;
    int age;
    };
    mankind :: mankind(string s, int t)
    {
    name=s;
    age=t;
    cout << "your name is: " << name << endl;
    cout << "your age is: " << age << endl;
    }


    thanx for any explanations~
    Never end on learning~

  4. #19
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    You may be new to the idea, but you've gotten good advice so far. Before you give up, look at the examples posted to your question so far, then try to figure it out yourself.
    Then, if it still doesn't work, post some code and ask for help then. Most people here are glad to help, but want to see that you're trying first.
    Truth is a malleable commodity - Dick Cheney

  5. #20
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by salvelinus
    You may ... you're trying first.
    I tried but mistakes comes~

    here is the declaration(which in myClass.h) of myClass:
    PHP Code:
    class mankind
    {
      public:
        
    mankind(string,int);
      private:
        
    string name;
        
    int age;
    }; 
    and this is for the relative definations(in the file myClass.cpp):
    PHP Code:
    #include <iostream>
    #include <string>

    mankind :: mankind(string sint t)
    {
      
    name=s;
      
    age=t;
      
    cout << "your name is: " << name << endl;
      
    cout << "your age is: " << age << endl;

    at last, I want to invoke myClass in another files, the code is as follows:

    #include <iostream>
    #include <string>

    #include "myClass.h"

    void main()
    {
    mankind black("black",189);
    }


    that'all, I write the codes with my comprehension. but there is still something wrong, I couldnt figure it out at once, what's that please ???
    Never end on learning~

  6. #21
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225
    black I think, considering that you're still a novice in managing a class, there's nothing wrong in what you did. After all, when you declared your class in main(), the constructor is still invoked the way a function would be.

    But if you want it to become clean cut, you could have done this:

    PHP Code:
    class mankind
    {
      public:
        
    mankind(string,int);
        
    void whoami(void)
      private:
        
    string name;
        
    int age;
    };

    mankind :: mankind(string sint t)
    {
      
    name=s;
      
    age=t;
    }

    void mankind::whoami(void)
    {
      
    cout << "your name is: " << name << endl;
      
    cout << "your age is: " << age << endl;
    }


    #include <iostream> 
    #include <string> 

    void main() 

      
    mankind black("black",189);

      
    black.whoami();

    You can digest this till you get the concept. There is no deadline in learning C++ just for pleasure.
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  7. #22
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Hey Billholm, I know how to use my class into main. this is my original code below:

    PHP Code:
    #include <iostream>
    #include <string>

    class mankind
    {
      public:
        
    mankind(string,int);
      private:
        
    string name;
        
    int age;
    };
    mankind :: mankind(string sint t)
    {
      
    name=s;
      
    age=t;
      
    cout << "your name is: " << name << endl;
      
    cout << "your age is: " << age << endl;
    }
    void main()
    {
      
    string a;
      
    int b;
      
    cout << "please input your name: ";
      
    cin >> a;
      
    cout << "please input your age: ";
      
    cin >> b;
      
    cout << "waiting please......" << endl;
      
    mankind man(a,b);
      
    cin >> "what";

    And now, I want my own class to be something just like those in std library, goti it ?
    Never end on learning~

  8. #23
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Originally posted by salvelinus
    Well, that's a good illustration. No destructor (can't remember if the compiler defaults it if not included). But shouldn't your #include <iostream.h> statement be in the myclass.cpp file?
    Sorry that often happens when I write code on forums. I change it alot as I go along and often forget to move things, that wont compile without the changes you mentioned and perhaps a few more

    A destructor is provided anyway if not defined, as is the default constructor. I didnt write it because it wasn't necessary for the example. I assume black has got a book informing him about class desgin and thought this was a question about separating files

    Edit : Sorry again, I agree with people far too easily and accept that I am wrong. This is not the case with the example above, it should work fine
    Last edited by endo; 06-25-2002 at 09:26 AM.

  9. #24
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Originally posted by black


    I tried but mistakes comes~

    here is the declaration(which in myClass.h) of myClass:
    PHP Code:
    class mankind
    {
      public:
        
    mankind(string,int);
      private:
        
    string name;
        
    int age;
    }; 
    and this is for the relative definations(in the file myClass.cpp):
    PHP Code:
    #include <iostream>
    #include <string>

    mankind :: mankind(string sint t)
    {
      
    name=s;
      
    age=t;
      
    cout << "your name is: " << name << endl;
      
    cout << "your age is: " << age << endl;

    at last, I want to invoke myClass in another files, the code is as follows:

    #include <iostream>
    #include <string>

    #include "myClass.h"

    void main()
    {
    mankind black("black",189);
    }


    that'all, I write the codes with my comprehension. but there is still something wrong, I couldnt figure it out at once, what's that please ???

    As far as I can see the only definite problem here is that myclass.cpp does not include myclass.h, in the same way that the main file does. You might encouter a few problems with using iostream not iostream.h and string not string.h.

    What errors are you getting, post them here and its much easier to solve your problems

  10. #25
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    If using the current standard of #includes, like #include <iostream>, you also need a using statement, either using namespace std; or using std::cout;
    Otherwise, each time you use cout you'd have to code it as std::cout << "blah blah"; Some people code that way, but I can't see why.
    Truth is a malleable commodity - Dick Cheney

  11. #26
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by salvelinus
    If using the current standard of #includes, like #include <iostream>, you also need a using statement, either using namespace std; or using std::cout;
    Otherwise, each time you use cout you'd have to code it as std::cout << "blah blah"; Some people code that way, but I can't see why.
    that is using namespace std; right ?
    I add this line to my file but mistake comes yet. the modified code is right below:

    PHP Code:
    #include <iostream>
    #include <string>
    #include "myClass.h"

    using namespace std;

    void main()
    {
      
    mankind black("black",999);

    the compiler report is:
    [Linker error] undefined reference to `mankind::mankind(basic_string<char, string_char_traits<char>, __default_alloc_template<false, 0> >, int)'

    what's wrong here ???
    Never end on learning~

  12. #27
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Did you #include "myclass.h" in the myclass.cpp file as well as in the .cpp file with main?
    Add the using statement to myclass.cpp as well, since you use cout there as well.
    You might have a classpath problem if your files are in other than expected directories, but that's a long shot.
    Main should be int main(), not void, and return 0 (zero). Check the FAQ's and search earlier posts for why.
    Truth is a malleable commodity - Dick Cheney

  13. #28
    Registered User
    Join Date
    Jun 2002
    Posts
    44

    Cool how could i use my own class ?

    Speaking of compilers:
    Is the Digital Mars compiler(on this web site's compiler resource section) capable of making DOS stand-alone programs(the only true programs for MS systems)?I mean TODALY stand-alone,no stubs,necessary files,etc.? And whats the diference from a Junior/Senior member?

  14. #29
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225
    >>And now, I want my own class to be something just like those in std library, goti it ?

    Oh I get it! You want to store your classes in some other file with .h extensions. Actually our colleagues here have already done a good job of explaining it. Just let it sink in for a while.

    Nonetheless, I'll provide you with a link for my previously posted source code for my assorted program:

    Utility.zip thread

    There are two utility.zip there choose the second , far down the first page of the thread.

    I stored my classes there in different .h files. You may want to take a look at the main .cpp source codes about how I used them.
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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