找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1997|回复: 8

[原创]构造函数

[复制链接]
发表于 2009-11-3 00:25:02 | 显示全部楼层 |阅读模式 IP:江苏扬州
#include<iostream.h>
class Student
{
public:
Student()
{
cout << "constructing student. \n";
semesHours =100;
gpa=3.5;
}
//其他公共成员
protected:
int semesHours;
float gpa;
};
class Teacher
{
public:
Teacher()
{
cout << "constructing teacher. \n";
}
};
class TutorPair
{
public:
TutorPair()
{
cout << "constructing tutorpair. \n";
noMeetings =0;
}
protected:
Student Student;
Teacher teacher;
int noMeetings; //会晤次数
};
void main()
{
TutorPair tp;
cout << "back in main. \n";
}
发表于 2009-11-3 00:25:09 | 显示全部楼层 IP:江苏扬州
析构函数


#include<iostream.h>
class Student
{
public:
Student()
{
cout << "constructing student. \n";
semesHours =100;
gpa=3.5;
}
~ Student()
{
cout <<"destructing student. \n";
}
//其他公共成员
protected:
int semesHours;
float gpa;
};
class Teacher
{
public:
Teacher()
{
cout << "constructing teacher. \n";
}
~Teacher()
{
cout << "constructing teacher. \n";
}
};
class TutorPair
{
public:
TutorPair()
{
cout << "constructing tutorpair. \n";
noMeetings =0;
}
~TutorPair()
{
cout << "constructing tutorpair. \n";
}
protected:
Student Student;
Teacher teacher;
int noMeetings;
};
void main()
{
TutorPair tp;
cout << "back in main. \n";
}
回复

使用道具 举报

发表于 2009-11-3 00:25:24 | 显示全部楼层 IP:江苏扬州
#include <iostream.h>
class Sample
{
private:
int count;
public:
Sample()
{ count=8; }
void Disp()
{
cout<<count<<endl;
}
void operator ++()
{
count++;
}
};
void main()
{
Sample obj;
obj++;
obj.Disp();
}
回复

使用道具 举报

发表于 2009-11-3 00:25:34 | 显示全部楼层 IP:江苏扬州
#include <iostream.h>
class Sample
{
private:
int count;
public:
Sample()
{ count=8; }
void Disp()
{
cout<<count<<endl;
}
Sample operator ++()
{
Sample temp;
temp.count =count+1;
return temp;
}
};
void main()
{
Sample obj1,obj2;
obj2=obj1++;
obj2.Disp();
}
回复

使用道具 举报

发表于 2009-11-3 00:25:43 | 显示全部楼层 IP:江苏扬州
#include <iostream.h>
class Sample
{
private:
int count;
public:
Sample()
{ count=8; }
void Disp()
{
cout<<count<<endl;
}
Sample operator ++()
{
count=count+1;
return (*this);
}
};
void main()
{
Sample obj1,obj2;
obj2=obj1++;
obj2.Disp();
}
回复

使用道具 举报

发表于 2009-11-3 00:25:54 | 显示全部楼层 IP:江苏扬州
#include <iostream.h>
class Sample
{
private:
int count;
public:
Sample()
{ count=8; }
void Disp()
{
cout<<count<<endl;
}
Sample operator +(Sample obj)
{
Sample temp;
temp.count =count+obj.count ;
return temp;
}
};
void main()
{
Sample obj1,obj2,obj3;
obj3=obj1+obj2;//->obj3=obj1.+(obj2);
obj3.Disp();
}
回复

使用道具 举报

发表于 2009-11-3 00:26:04 | 显示全部楼层 IP:江苏扬州
#include <iostream.h>
class Sample
{
private:
int count;
public:
Sample()
{ count=8; }
void Disp()
{
cout<<count<<endl;
}
Sample operator +(Sample obj)
{
count=count+obj.count;
return (*this);
/* Sample temp;
temp.count =count+obj.count ;
return temp;
*/
}
};
void main()
{
Sample obj1,obj2,obj3;
obj3=obj1+obj2;//->obj3=obj1.+(obj2);
obj3.Disp();
}
回复

使用道具 举报

发表于 2009-11-3 00:26:12 | 显示全部楼层 IP:江苏扬州
#include <iostream.h>
#include <string.h>
class String
{
private:
char *p;
public:
String (){}
String(char *ptr)
{
p=new char[strlen(ptr)];
strcpy(p,ptr);
}
String operator +(String s)
{
String temp;
temp.p =strcat(p,s.p );
return temp;
}
void Disp()
{
cout<<p<<endl;
}
};
void main()
{
String s1="Hello";
String s2=" world!";
String s3;
s3=s1+s2;
s3.Disp ();
}
回复

使用道具 举报

发表于 2009-11-3 00:26:21 | 显示全部楼层 IP:江苏扬州
#include <iostream.h>
class Base{
protected:
int a,b;
public:
Base(){a = 0;} //默认构造函数
Base(int c,int d){ a = c;b=d;} //单参数构造函数
};
class Derived : public Base{
public:
int d;
Derived(): Base(){} //默认构造函数
Derived(int c): Base(c,4){}
void Disp()
{
cout<<b<<endl;
}
};
void main()
{
Derived obj(3);
obj.Disp ();

}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 03:32 , Processed in 0.250507 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表