PDA

View Full Version : حرفه ای: کلاس



suraty
جمعه 29 مهر 1390, 13:00 عصر
سلام
مشکل این برنامه چیه؟
چرا خطا میده؟:ناراحت:


#include"iostream.h"
#include"conio.h"
class class1{
int c;
public:
class1(int a){
c=a;
cout<<c;
}
};
int main(){
class1 ob1(5);
ob1(7);
return 0;
}

m.soleimani
جمعه 29 مهر 1390, 13:38 عصر
یکم برات تر و تمیزترش کردم نگاه کنی خودت متوجه مشکلات کد قبلی خواهی شد :

#include <iostream>
using namespace std;
class Test
{
private:
int c;
public:
Test(int a)
{
c = a;
}

void GetVar(int x)
{
c = x;
}

void ShowVar()
{
cout << c << endl;
}
};
int main()
{
Test obj(5);
obj.ShowVar();
obj.GetVar(7);
obj.ShowVar();
system("pause");
return 0;
}