Strange class statement...

This is a discussion on Strange class statement... within the C++ Programming forums, part of the General Programming Boards category; While I was looking at an example of class operations I saw something I never seen before. Where the constructor ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    162

    Strange class statement...

    While I was looking at an example of class operations I saw something I never seen before. Where the constructor for class Position is declared there is a :x(xx),y(yy) statement and I cant understand what it is doing. I seems like it is using float x and y as functions, and this even before the {}. Please explain this statement, what it does, how it does it, what else strange statements like this you can do and if there is any where I can read more about this?

    Code:
    #include<iostream.h>
    
    class Position {
    	float x,y;
    public:
    	Position(float xx, float yy=0):x(xx),y(yy) {}
    	Position operator + (const Position& e) const;
    	Position operator - (const Position& e) const;
    	
    	void Display(void)
    	{ cout << "(" << x << ", " << y << ")" << endl; }
    	void Input(void)
    	{ cout << "enter x och y:"; cin >> y;; }
    };
    
    Position Position::operator + (const Position& e) const
    {
    	Position res = *this;
    	res.x += e.x;
    	res.y += e.y;
    	return (res);
    }
    
    Position Position::operator - (const Position& e) const
    {
    	Position res = *this;
    	res.x -= e.x;
    	res.y -= e.y;
    	return (res);
    }
    We haven't inherited Earth from our parents; instead we have borrowed her from our children - old Indian saying.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    class A
    {
    public:
       A() : x(0),y(0) {}
    private:
       int x;
       int y;
    };
    
    class B
    {
    public:
       B() { x = 0; y=0; }
    private:
       int x;
       int y;
    };
    are identical in meaning.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 12:36 PM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 03:46 PM
  4. Strange problems with string class
    By creativeinspira in forum C++ Programming
    Replies: 10
    Last Post: 07-18-2007, 02:11 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

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