Nov 19, 2011 at 7:26pm Nov 19, 2011 at 7:26pm UTC
hi all
how can we write this in c++?
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int x, y;
} * Point;
void init (Point p) {p->x = 0; p->y = 0;}
void set (Point p, int a, int b) {p->x = a; p->y = b;}
void showw (Point p) {printf("%d %d\n", p->x, p->y);}
main(){
Point p = (Point) malloc(8),
q = (Point) malloc(8);
init(p);
set(q, 1, 2);
showw(p);
printf("%d %d\n", q->x, q->y);
}
thanks in advance
Nov 19, 2011 at 7:40pm Nov 19, 2011 at 7:40pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include <iostream>
#include <cstdlib>
using namespace std;
// no longer a pointer typedef
class Point {
private :
int m_x;
int m_y;
public :
Point() : m_x(0), m_y(0) {}
Point(int x, int y) : m_x(x), m_y(y) {}
void clear() // was init
{
m_x = 0; m_y = 0;
}
void set(int x, int y)
{
m_x = x; m_y = y;
}
int getX() const
{
return m_x;
}
int getY() const
{
return m_y;
}
void show() const
{
cout << m_x << " " << m_y << endl;
}
};
ostream& operator <<(ostream& os, const Point& p)
{
os << p.getX() << " " << p.getY();
return os;
}
int main(){
Point* p = new Point;
Point* q = new Point(1, 2);
Point* r = new Point;
r->set(3, 4);
p->show();
cout << q->getX() << " " << q->getY() << endl;
cout << *r << endl;
delete p; // C version should have been calling free() !
delete q;
delete r;
return 0;
}
Last edited on Nov 19, 2011 at 7:58pm Nov 19, 2011 at 7:58pm UTC
Nov 19, 2011 at 7:46pm Nov 19, 2011 at 7:46pm UTC
thanks for help
but what about malloc(8)??
Last edited on Nov 19, 2011 at 7:47pm Nov 19, 2011 at 7:47pm UTC
Nov 19, 2011 at 7:54pm Nov 19, 2011 at 7:54pm UTC
No need. operator new knows how big class Point is.
Point* p = new Point(1, 2);
does the same work as
1 2
Point* p = (Point*) malloc(sizeof (Point)),
set(p, 1, 2);
Inidentally, the C could be better as
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int x, y;
} Point, * PointPtr; // or you could just use Point*
void init (PointPtr p) {p->x = 0; p->y = 0;}
void set (PointPtr p, int a, int b) {p->x = a; p->y = b;}
void showw (PointPtr p) {printf("%d %d\n" , p->x, p->y);}
main(){
PointPtr p = (PointPtr) malloc(sizeof (Point)),
q = (PointPtr) malloc(sizeof (Point));
init(p);
set(q, 1, 2);
showw(p);
printf("%d %d\n" , q->x, q->y);
free(p);
free(q);
}
That is, typedef the struct to Point and the pointer to PointPtr, so you can use sizeof(Point), rather than 8. Then if you add a new member to Point (e.g. color) you don't have to touch the code to change the size passed to malloc.
Last edited on Nov 19, 2011 at 7:57pm Nov 19, 2011 at 7:57pm UTC
Nov 19, 2011 at 8:17pm Nov 19, 2011 at 8:17pm UTC
thanks once again
But
we can write:
Point* p = (Point*) malloc(8*sizeof(Point),set(p,1,2));
??
by the way what is the difference between
Point* p= (Point) malloc(8)
and
Point* p= (Point) malloc(8*sizeof(Point))
isn'it the same??
Nov 19, 2011 at 8:27pm Nov 19, 2011 at 8:27pm UTC
1. no - malloc()
takes only one param: the number of bytes you want to allocate
2. Point* p = (Point)malloc(8);
is a typo. The cast must be the same type
Point p = (Point)malloc(8);
or
Point* p = (Point*)malloc(8);
3. Also a typo, due to the cast
But
Point* p= (Point*) malloc(8*sizeof (Point));
allocates enough space for 8 points. That is, it allocates an array of 8 points.
Last edited on Nov 19, 2011 at 8:51pm Nov 19, 2011 at 8:51pm UTC
Nov 19, 2011 at 8:40pm Nov 19, 2011 at 8:40pm UTC
thank you once again
the 8 was the size of the two integers x and y (sizeof(int)=4)
Nov 19, 2011 at 8:53pm Nov 19, 2011 at 8:53pm UTC
I could see what 8 meant in your code. But I always use sizeof() to achieve the same aim myself.
Last edited on Nov 19, 2011 at 8:53pm Nov 19, 2011 at 8:53pm UTC
Nov 19, 2011 at 9:01pm Nov 19, 2011 at 9:01pm UTC
I know this isn't what they were asking for but I'm wondering; could they just swap out <stdio.h> & <stdlib.h> for <cstdio> & <cstdlib>?
[edit]: And 'using namespace std;' too.
Last edited on Nov 19, 2011 at 9:02pm Nov 19, 2011 at 9:02pm UTC
Nov 19, 2011 at 11:42pm Nov 19, 2011 at 11:42pm UTC
If you're writing pure C, and have a .c extension, then you cannot use namespaces. Hence <cstdio>, etc are unusable (they just wrap <stdio.h>, etc in the std namespace anyway)
Nov 20, 2011 at 4:48pm Nov 20, 2011 at 4:48pm UTC
I mean to compile it as C++.
Nov 20, 2011 at 10:43pm Nov 20, 2011 at 10:43pm UTC
If you're compiling as C++, then it makes no odd if you use <stdio.h>, etc or <cstdio>, etc plus using namespace std.
I would prob. do the former if I'm just using C functions. I switch to <cstdio>, etc when I'm including things like <string>, <vector>, etc; to be consistent.
Last edited on Nov 20, 2011 at 10:44pm Nov 20, 2011 at 10:44pm UTC