首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

两个有关问题

2012-03-30 
两个问题!#includeiostreamusingstd::coutusingstd::cinclassCat{public:Cat(int)~Cat()intgetage()

两个问题!
#include   <iostream>

using   std::cout;
using   std::cin;
class   Cat
{
public:
Cat   (int);
~Cat   ();
int   getage();
void   setage(int   age);
void   moew();
private:
int   itsage;
};

Cat::Cat(int   a)
{
itsage=a;
}
Cat::~Cat()
{
}
int   Cat::getage   ()
{
return   itsage;
}
void   Cat::setage   (int   age)
{
itsage=age;
}
void   Cat::moew()
{
cout < < "moew..\n ";
}
int   main()
{int   age;
cin> > age;
Cat   ketty(age);
ketty.moew();
cout < < "ketty   is   " < <ketty.getage   () < < "   years   old.\n ";
ketty.moew();
ketty.setage(7);
cout < < "now   ketty   is   " < <ketty.getage() < < "   yeas   old.\n ";
ketty.~Cat;
Cat   *pketty=new   Cat(age);
cout < < "ketty   is   " < <pketty-> getage   () < < "   years   old.\n ";
pketty-> setage   (15);
cout < < "ketty   is   " < <pketty-> getage   () < < "   years   old.\n ";
return   0;
}
这个程序其他正常,就是再第三句输出的getage()的返回值不管在age输入是几或者直接给常数,它的返回值都是1,这是为什么?

#include   <iostream>

using   std::cout;
using   std::cin;

class   rechteck
{
public:
rechteck(int   h,int   b);
~rechteck();
void   schade();
private:
int   hoch;
int   breite;
};
rechteck::rechteck(int   h,   int   b)
{
hoch=h;
breite=b;
}
rechteck::~rechteck   ()
{
}
void   rechteck::schade()
{int   i,j;
for(j=0;j <hoch;j++)
{
for(i=0;i <breite;i++)
{
cout < < "* ";
}
cout < < "/n ";
}
}  
int   main()
{
int   a;
int   b;
cin> > a;
cin> > b;
rechteck   r1(a,b);
r1.schade   ();
return   0;
}
这个程序无法通过编译,原因是
--------------------Configuration:   lei1   -   Win32   Debug--------------------
Compiling...
lei.cpp
Linking...
LIBCD.lib(wincrt0.obj)   :   error   LNK2001:   unresolved   external   symbol   _WinMain@16
Debug/lei1.exe   :   fatal   error   LNK1120:   1   unresolved   externals
执行   link.exe   时出错.
谢谢各位了,还没试过怎么给分,今天正好试下.


[解决办法]
关于指针int *a;和int* a;这两句话作用一样吗?
=================
如果只有上面的两个其实没什么区别
但是如果是下面这句的话就容易出现misunderstand了
int* a,b;
其实这句中a是指针而b不是,但是看起来好像a和b都是指针似的,呵呵
所以最好还是写成int *a;这种类型的
也就是这样:int *a,b;
这样看起来就清楚多了
[解决办法]
子系统设置为控制台项目不是Windows
alt+F7进setting
link属性页
在project options中把subsystem:windows改成subsystem:console
[解决办法]
int* a和int *a没什么区别都是一个指向int型变量的int* 型指针;
个人认为int* a比较好,可以直接看出指针类型,应用时并不易出错。
------解决方案--------------------


第一个代码不需要写ketty.~Cat;它会自动释放
对于Cat *pketty=new Cat(age); new以后要记得delete
至于就是再第三句输出的getage()的返回值不管在age输入是几或者直接给常数,它的返回值都是1
我调试的结果是没有问题

热点排行