سلام میخوام بدونم اگه چند تا struct تعریف کنیم و به عنوان مثال در structدوم نیاز به تعریف عنصری داشته باشیم که خود آن عنصر struct اول است چه کاری باید بکنیم:متفکر:
Printable View
سلام میخوام بدونم اگه چند تا struct تعریف کنیم و به عنوان مثال در structدوم نیاز به تعریف عنصری داشته باشیم که خود آن عنصر struct اول است چه کاری باید بکنیم:متفکر:
// An example about structs:
struct test1
{
int x,y;
};
struct test2
{
// define an struct in other struct
struct test3
{
int z;
};
int w,h;
struct test1 t1; // declare a variable of struct test1 in test2
struct test2 *t2; // declare a variable of struct test2 in it is own body
struct test3 t3; // declare a variable of inline struct test3
};
void main(void)
{
struct test1 mt1 = {2,4};
struct test2 mt2;
struct test2 mt2_2 = {0,0,0,0,0};
struct test2::test3 mt3;
mt2.w=1;
mt2.h=2;
mt2.t1.x=3;
mt2.t1.y=4;
mt2.t3.z=5;
mt2.t2 = &mt2_2;
mt2.t2->h = 2; // operate like mt2_2.h=2
mt2.t2->w = 2; // operate like mt2_2.w=2
mt3.z = 6
}
اما منظور من این نبود من میخوام بدنهی یکی از structها توی دیگری هم باشه مثلا یکstructبرای دانشجو با عناصر شماره دانشجویی و....داشته باشیم ویک struct دیگه کارت دانشجویی که عنصر اون شماره دانشجویی باشه
منظور شما ارث بریه؟
#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
typedef struct test1
{
protected:
int X;
public :
void SetX(int x)
{
X=x;
}
} Test1;
// struct test2 inherites variable X
// and function SetX from struct test1
typedef struct test2 : Test1
{
protected:
int Y;
int Z;
public:
void SetY(int y)
{
Y=y;
}
void Sum()
{
Z=X+Y;
}
int GetZ()
{
return Z;
}
} Test2;
int _tmain(int argc, _TCHAR* argv[])
{
Test2 t2;
t2.SetX(3);
t2.SetY(4);
t2.Sum();
printf("%d",t2.GetZ());
getch();
return 0;
}
اگه منظورتون اینه که داخل یه ساختار یه ساختار دیگه باشه خوب بفرما....
struct card{
char stdno[11];
char numbercard[10];
};
struct student{
char fname[30];
char lname[30];
int avg;
card c;
};
الان اطلاعات کارت در ساختار دانشجو قرار داده و شما اگه مثلا یه ساختار student تعریف کردید به نام a وخواستید شماره کارتشو وارد کنید مینویسید
a.c.numbercard=//hala har chi??!!
امیدوارم درست کمکتون کرده باشم
موفق باشید