Geometry.h
#include "iostream" using namespace std;
class Point
{
struct Coordinate;
public:
Point(float a, float b) {coord.x = a; coord.y = b;}
Coordinate GetCoord() const {return coord;}
private:
struct Coordinate{float x; float y;}coord;
};
注:
若要将GetCoord()的定义写在类外,需要这样写:
Point::Coordinate Point::GetCoord() const {return coord;}
Geometry.cpp
// Geometry.cpp : 定义控制台应用程序的入口点。//
#include "stdafx.h"
#include "iostream"
#include "Geometry.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Point p(2, 5);
cout<<p.GetCoord().x<<","<<p.GetCoord().y<<endl;
return 0;
}
本文探讨了如何在C++中使用结构体作为类的成员变量,详细阐述了如何在外部文件中定义类成员函数GetCoord(),通过实例展示了其用法,加强了对C++类和结构体结合的理解。
1436

被折叠的 条评论
为什么被折叠?



