Thread: objects

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    86

    objects

    I know how to define classes, and methods, both public and private and all that stuff.

    I do not understand two things about classes

    how do classes do things like making a variable type, such as the string class

    my other question is best served by an example:

    Code:
    class example {
    public:
    
    int variable1;
    char variable 2[100];
    
    int method(void){
    
    //do things
    
    }
    
    };
    
    example randomFunction(void);
    how do I structure the return statement with randomFunction?

    I'm sorry if this is a question I'm going to take flack for, but neither of my c++ books has any info on it and my web searches turned up nothing, as did my searches of the tutorials on this site

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    either i am just too tired or i really dont understand your questions :/


    how do classes do things like making a variable type, such as the string class

    what do you mean by making a variable type?
    and what do you mean by structuring the return statement?
    signature under construction

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    I think what he is trying to get at, is how do objects increase and decrease in size at runtime (dynamic allocation most of the time).

    Look into dynamic memory allocation, the operators new, delete, and delete[] are the operators included as part of the C++ spec.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    the return statement is the easy one to explain, my bad for being not clear

    by "structuring the return statement", I am reffering the line

    Code:
    return variable;
    if I have a class with the public variables int variable1 and bool variable2, how would I return that

    like this? return {int something,bool something};

    what I meant about creating a variable is this

    the variable string is not built into the c++ language

    you have to include string.h

    it is my understanding that string.h contains a class, which defines the object string

    I dont have to say:

    Code:
    string str;
    
    str.stringToUse="hello, world!";
    I can just use

    Code:
    string str=use;
    how do I do that?

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    crap! I meant
    Code:
    string str="hello, world!";

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I think what he is trying to get at, is how do objects increase and decrease in size at runtime
    I seriously doubt that.

    how do classes do things like making a variable type, such as the string class
    With a lot of tricky programming.

    how do I structure the return statement with randomFunction?
    ...return a variable of type example.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    basically there are only following primitive types:
    char, short, int, long, float, double

    actually there are even only 2 types: integers and floating points (just the size differs)
    everything is composed of those primitive types.

    the trick for some composed thingy (e.g. a class) to act like an ordinary built in type is called operator overloading.
    that means even though the syntax looks similar or even the same - no matter wether youre using integers or string - the semantics behind it differ because the operators have a different meaning. the operators for strings are more complicated than those for ordinary integer arithmetic. (in fact the operators for integer arithmetic are built in)
    signature under construction

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    class Apple
    {
    public:
    	int num;
    	
    	Apple(int n)
    	{
    		num = n;
    	}
    	
    	Apple() 
    	{
    		num = 1;
    	}
    };
    
    int main()
    {
    	Apple a(10);
    	cout<<a.num<<endl;
    
    	Apple b;
    	cout<<b.num<<endl;
    	
    	return 0;
    }

  9. #9
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    wat the hell was that supposed to be?

  10. #10
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    actually there are even only 2 types: integers and floating points (just the size differs)
    everything is composed of those primitive types.
    is there a tutorial somewhere on how to do this

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    if I have a class with the public variables int variable1 and bool variable2, how would I return that

    like this? return {int something,bool something};
    You don't return 'classes'. You return 'objects' of the 'class type'. The 'thing' listed prior to the function name is the 'return type' of the function. If you are going to return a variable of type int or an int 'literal'(e.g. 1, 100, 30), you put int in front of the function name. If you are going to return an object of your class, you put the class name in front of the function name. Then, inside the function, you return a variable of your class type:
    Code:
    class Apple
    {
    public:
    	int num;
    	double size;
    	
    	Apple(int n, double d)
    	{
    		num = n;
    		size = d;
    	}
    
    };
    
    Apple func(void)
    {
    	Apple my_apple(10, 3.5);
    	return my_apple;
    }
    
    int main()
    {
    	
    	Apple a = func();
    	cout<<a.num<<endl;
    	cout<<a.size<<endl;
    
    	return 0;
    }
    Last edited by 7stud; 02-27-2005 at 09:42 PM.

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    the trick for some composed thingy (e.g. a class) to act like an ordinary built in type is called operator overloading.
    is there a tutorial somewhere on how to do this
    You can find many tutorials on 'operator overloading' using google.

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    wat the hell was that supposed to be?
    lol. There are two parts to this statement:

    string str="some text";

    1) First a string object is created called str.

    2) Operator overloading has been defined for a string object such that when there is an equals sign on the right side of a string object, certain lines of code are executed, which 'attach' the string literal to the variable str.

    In case you were wondering about the syntax with 1):

    string str;

    my post addressed that.

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>"the variable string is not built into the c++ language.
    you have to include string.h.
    it is my understanding that string.h contains a class, which defines the object string."


    The word string can be very confusing when used in context of C/C++. The two most common usages are to indicate 1) a null terminated char array (so called C style string) OR 2) an object of the STL string class. Objects of the STL string class are declared like this:

    string str;

    To use the string class you must include the string header file, like this:

    #include <string>

    and be prepared to use a namespace indicator like one of the following options:

    1) add the line----using namespace std;-----after the list of #includes to bring all of namespace std into scope. This is the "easiest" way for beginners.

    2) add the line----using std::string;-----after the list of #includes to bring just the string class from namespace std into scope.

    3) preface each declaration of a string object with the std:: scope resolution prefix, like this:

    std::string str;


    The string.h header contains functions that manipulate C style strings and has nothing to do with STL string class or objects of the STL string class. In uptodate compilers the string.h file has been updated to cstring and you are discouraged from using the string.h any longer.

    To make matters worse, C style strings and objects of the STL string class are related in that each STL string object has an embedded C style string that can be accessed use the c_str() method and C style strings can be converted into STL strings using a constructor. For example:

    #include <string> //for the STL string class
    #include <cstring> //for strcpy

    char cString[] = "hello ";
    std::STLstring(cString);//convert C string to STL string
    char str2[14];
    strcpy(str2, STLstring.c_str());//convert STL string to C string. No real reason to do this UNLESS you want to manipulate str2 using methods in cstring. All (I believe anyway) the functions in cstring are available in one form or another in the STL string class or the std::algorithm class.
    Last edited by elad; 02-28-2005 at 10:33 AM.
    You're only born perfect.

  15. #15
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    I looked into operator overloading and learned hwo to do that, and it's been very helpful

    in case it helps anyone with the next question I should mention that the data structure I'm trying to write is a hash (any perl programmer will know what I'm talking about)

    How do you specify vairable type like in the vector class...ie: how do you overload the <> operators such that they accept a vairable type?

    Code:
    Hash <int>
    stuff like that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. most efficient way to write filestream objects?
    By darsunt in forum C++ Programming
    Replies: 3
    Last Post: 01-26-2009, 05:17 PM
  2. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  3. Question about cout an stack object?
    By joenching in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2005, 10:10 PM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM