-1

I came across this code, and I was interested what the constructs marked below by comments //<-- This are.

If it has a name then I would like to know (to google it and get more info if possible).

#include <stdio.h>
typedef struct point {
 float x,y;
 void print(void);
} dot;

typedef struct rect {
  dot pt1,pt2;
  float area(void);
  dot center(void);
  void print(void);
} rectangle;

void dot::print(void){ //<-- This
  printf("(%3.1f,%3.1f)", x, y);
}

void rectangle::print(void){ //<-- This
  pt1.print(); printf(":"); pt2.print(); 
}

dot rectangle::center(void){ //<-- This 
  dot c; c.x=(pt1.x + pt2.x)/2;
  c.y=(pt1.y + pt2.y)/2; return c;
}

float rectangle::area(void){ //<-- This
  return((pt2.x-pt1.x)*(pt2.y-pt1.y)); 
}
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • 3
    Those are member functions. Can you be more clear on what exactly about them you do not understand? – Retired Ninja Feb 05 '17 at 00:01
  • I think the OP just want to know the name of it, so he can google for that. – cuongptnk Feb 05 '17 at 00:02
  • 6
    Almost every part of this code is really terrible. You're better off reading a good book and learning a decent version of C++. – Kerrek SB Feb 05 '17 at 00:02
  • As I see from this example the void rectangle::print(void) can access the pt1.print() directly, what is the connection of this "rectangle::print", "structure rect" and ''dott::print" @RetiredNinja – Math Newbie Feb 05 '17 at 00:03
  • I've never seen definitions of member functions disambiguated by a typedef name. It's very confusing. – owacoder Feb 05 '17 at 00:03
  • This looks *c-ish*. Is this C rather than C++? –  Feb 05 '17 at 00:06
  • @RawN `::` is a syntax error in C. – melpomene Feb 05 '17 at 00:06
  • The syntax is odd, but it compiles. http://ideone.com/YOxYov That doesn't make it any less terrible. :) – Retired Ninja Feb 05 '17 at 00:06
  • @melpomene I see. Good to know. –  Feb 05 '17 at 00:07
  • @RawN yeah realised just now it's C, however it says that it can be used only in C++, scratching my head rn :P – Math Newbie Feb 05 '17 at 00:07
  • 2
    @MathNewbie `point` and `rect` are classes (the `struct` keyword declares classes in C++); `print`, `center`, `area` are methods. – melpomene Feb 05 '17 at 00:08
  • Actually i cannot find any google link say "<-- This" is member function. Can anyone explain ? – cuongptnk Feb 05 '17 at 00:10
  • @cuongptnk with (<-- This ) I'm just pointing part of the code, sorry if it was confusing .. Should have put it as a comment – Math Newbie Feb 05 '17 at 00:13
  • @MathNewbie LOL, i have been spending 15 mn to look for what that means. – cuongptnk Feb 05 '17 at 00:14
  • @cuongptnk My unsuccessful attempt to upgrade C++, lol – Math Newbie Feb 05 '17 at 00:17
  • This code is definitely C++, *not* C. I'd call it C++ written with a bad C accent. – Keith Thompson Feb 05 '17 at 00:50
  • Your book explains these. Carry on reading. And, no, you can't learn C++ by randomly Googling things you've never seen before. – Lightness Races in Orbit Feb 05 '17 at 00:51
  • @LightnessRacesinOrbit - Didn't say I have seen it in book where it is explained.. I came across this example in one presentation where it is not explained, and yeah how would I know what it is if I haven't seen something like that (coming from C background where this doesn't exist).. Btw Google helps, not that I will learn everything from it.. – Math Newbie Feb 05 '17 at 15:40
  • Your C++ book definitely explains what member functions are and how to write/use them. – Lightness Races in Orbit Feb 05 '17 at 20:31
  • @LightnessRacesinOrbit I didn't see it in a book... Thanks.. Read the last comment again – Math Newbie Feb 08 '17 at 22:57
  • Then you need a better book. Here are some recommended ones: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Lightness Races in Orbit Feb 09 '17 at 01:29
  • Hey @MathNewbie I just want to say that it works really well to learn programming form hacking and modifying existing code. It really helps in understanding how the code, and the machine works. But it doesn't really teach you much about code style and stuff. So Keep hacking. And don't take people on SO too seriously ;) – David van rijn Mar 15 '17 at 13:02
  • @Davidvanrijn Hey, thanks man .. Already know much more advanced stuff than this, just need little push :D !! – Math Newbie Mar 17 '17 at 20:28

2 Answers2

2

They are implementations of the functions defined in the classes (structs) abouse. Usually though, you would do this in your cpp file, so you would have your h file with:

class Foo{
     int method1();
     int method2();
}

and then in your cpp file you would add the implementation using:

int Foo::method1(){
   ....
}

This code is a bit silly though, because the classes are defined in ye olde c way using the typedef struct syntax. This would make sense in some cases, because c code is also valid c++ so you could have code that compiled as both. However c++ is not always valid c and this code id definitely c++ because of the member functions, so there is no point in using the typedef struct syntax. It is probably old code that has been modified.

David van rijn
  • 2,108
  • 9
  • 21
0

The lines you are pointing to refer to the declaration of a function. I will explain one of these lnes, because you can apply the same lgic to the rest of them.

Let's look at the line:

    void dot::print(void){

The first word in this line, void, defines the type of data returned fromthe function. Since it is void, no value is returned from this function, which is evident fomthe fact that there is no return statement in the entire function.

    void dot::print(void) {
        printf("(%3.1f,%3.1f)", x, y); // this is the last line of the function. This function does not pass on any value or data
    }

Next is dot::, which is an object of struct point. If you see after the closing } of the struct point, you wil see that dot is declared here.

For the object dot, there is a function declaration called print(). This function is defined here, but since we have to indicate that we have to indicate that print() is a member of dot, we add the dot:: before the print(void) in the declaration.

Lastly is the void in parenthesis. This simply means that the function has no input parameters from the function that has called it; in other words, it does not need any data from outside the function.


Just as a recommendation, your code is more c than c++. You would be better off tagging this question as c instead of c++.

BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31