Newb requests help with basic syntax

This is a discussion on Newb requests help with basic syntax within the C++ Programming forums, part of the General Programming Boards category; Hi all. This is my first attempt a OO in C++. I'm sure there are some obvious mistakes. I know ...

  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
    Dae
    Dae is offline
    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
    21,914
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,914
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,783
    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, 03:49 AM
  5. Zipping files
    By CompiledMonkey in forum C Programming
    Replies: 19
    Last Post: 03-06-2003, 11:23 AM

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