Thread: Newb requests help with basic syntax

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    30

    Newb requests help with basic syntax

    Hi all.

    This is my first attempt a OO in C++. I'm sure there are some obvious mistakes. I know there must be more than one way to do this, but I would like to follow a partial example I was given and leave the first 15 lines unchanged (if possible) and continue using stdio.h too.

    I might respond with slightly different approaches for critiquing in the thread too; the key point is I'm just trying to learn.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    class SomeClass{
    	private:
    		int x;
    		int y;
    		int z;
    		
    	public:
    		SomeClass(int x, int y, int z){ 
    			this->x = x;
    			this->y = y;
    			this->z = z;
    		}
    		
    	void print(SomeClass z){
    		print("%d %d %d\n", z.x, z.y, z.z); 
    	}
    };
    		
    	int main(){
    
    		SomeClass a (1, 2, 3);
    		SomeClass b (4, 5, 6);
    	
    		print(a);
    		print(b);
    	
    		return 0;
    }
    Thanks in advance.

  2. #2
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    - private is implicit
    - don't need return 0
    - print text should be printf, or use c++ cout
    - print doesn't need arguments if its inside the class, if its outside u need to "friend" it
    - indentation should line up

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    class SomeClass
    {
        int x;
        int y;
        int z;
    
        public:
            SomeClass(int x, int y, int z)
            {
                this->x = x;
                this->y = y;
                this->z = z;
            }
    
            void print()
            {
                printf("%d %d %d\n", x, y, z);
            }
    };
    
    
    int main()
    {
        SomeClass a(1, 2, 3);
        SomeClass b(4, 5, 6);
    
        a.print();
        b.print();
    }
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Is it not necessary to always return 0 when main() is of type int?

    I thought it was always good to return 0 to signify that main() had executed successfully.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Swerve
    Is it not necessary to always return 0 when main() is of type int?
    The return type of the global main function must be int, but explicitly returning 0 from it is optional.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    30
    Thank you Dae!

    Now, I have a follow up question if anyone can help...

    I would like to write a 'global function' that is *outside* of the class. This function should be able to work with two different objects. Here's what I tried (it does not compile):

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int someGlobalFunction(SomeClass x, SomeClass y);
    
    class SomeClass{
        private:
    		int x;
    		int y;
    		int z;
    
        public:
            SomeClass(int x, int y, int z)
            {
                this->x = x;
                this->y = y;
                this->z = z;
            }
    
            void print()
            {
                printf("%d %d %d\n", x, y, z);
            }
    };
    
    
    int main(){
        SomeClass a(1, 2, 3);
        SomeClass b(4, 5, 6);
    
        a.print();
        b.print();
    }
    
    int someGlobalFunction(SomeClass a, SomeClass b){
    		return a.getX() + b.getY + b.getZ();
    }
    
    SomeClass::getX(){return x}
    SomeClass::getY(){return y}
    SomeClass::getZ(){return z}

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Roger
    I would like to write a 'global function' that is *outside* of the class. This function should be able to work with two different objects. Here's what I tried (it does not compile):
    The problem is that you declared the function with SomeClass parameters, but SomeClass has not yet been declared. You could fix this with a forward declaration:
    Code:
    class SomeClass;
    
    int someGlobalFunction(SomeClass x, SomeClass y);
    or you could simply declare someGlobalFunction after defining SomeClass, since a class definition is also a class declaration.

    Given what you appear to be doing, this is one way of doing it:
    Code:
    #include <cstdio>
    
    class SomeClass {
    public:
        SomeClass(int x, int y, int z) : x(x), y(y), z(z) {}
    
        int getX() const;
        int getY() const;
        int getZ() const;
    private:
        int x;
        int y;
        int z;
    };
    
    void print(const SomeClass& obj);
    int someGlobalFunction(const SomeClass& x, const SomeClass& y);
    
    int main() {
        SomeClass a(1, 2, 3);
        SomeClass b(4, 5, 6);
    
        print(a);
        print(b);
    }
    
    void print(const SomeClass& obj)
    {
        std::printf("%d %d %d\n", obj.getX(), obj.getY(), obj.getZ());
    }
    
    int someGlobalFunction(const SomeClass& a, const SomeClass& b) {
        return a.getX() + b.getY + b.getZ();
    }
    
    // ...
    Notice that I have:
    • Included <cstdio> instead of <stdio.h>
    • Used a constructor initialisation list instead of assigning in the constructor body.
    • Declared the public members of the class before the private members.
    • Added in the missing declarations for getX() etc.
    • Changed print to be a free function since it can use the getter functions.
    • Pass SomeClass by const reference instead of by value since you do not actually need to copy.
    • Shifted the declaration of someGlobalFunction to after the class definition.


    My indentation is more consistent than yours too
    You should add in the getter function definitions and fix the compile errors that result.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    b.getY should be b.getY().
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  2. Replies: 14
    Last Post: 07-14-2009, 08:16 AM
  3. Writing all HTTP requests from a website to a log file
    By goomyman in forum C# Programming
    Replies: 1
    Last Post: 07-29-2005, 09:18 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Zipping files
    By CompiledMonkey in forum C Programming
    Replies: 19
    Last Post: 03-06-2003, 12:23 PM