| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 777 人关注过本帖
标题:[原创]构造函数
只看楼主 加入收藏
271391233
Rank: 1
等 级:新手上路
帖 子:174
专家分:0
注 册:2005-2-24
收藏
 问题点数:0 回复次数:9 
[原创]构造函数
#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";
}
搜索更多相关主题的帖子: 函数 构造 Student public 
2006-04-10 10:33
271391233
Rank: 1
等 级:新手上路
帖 子:174
专家分:0
注 册:2005-2-24
收藏
得分:0 
析构函数


#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";
}

坚持就是胜利>>静心,静思
2006-04-10 10:33
271391233
Rank: 1
等 级:新手上路
帖 子:174
专家分:0
注 册:2005-2-24
收藏
得分:0 
#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();
}

坚持就是胜利>>静心,静思
2006-04-10 10:34
271391233
Rank: 1
等 级:新手上路
帖 子:174
专家分:0
注 册:2005-2-24
收藏
得分:0 
#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();
}

坚持就是胜利>>静心,静思
2006-04-10 10:35
271391233
Rank: 1
等 级:新手上路
帖 子:174
专家分:0
注 册:2005-2-24
收藏
得分:0 
#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();
}

坚持就是胜利>>静心,静思
2006-04-10 10:35
271391233
Rank: 1
等 级:新手上路
帖 子:174
专家分:0
注 册:2005-2-24
收藏
得分:0 
#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();
}

坚持就是胜利>>静心,静思
2006-04-10 10:35
271391233
Rank: 1
等 级:新手上路
帖 子:174
专家分:0
注 册:2005-2-24
收藏
得分:0 
#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();
}

坚持就是胜利>>静心,静思
2006-04-10 10:36
271391233
Rank: 1
等 级:新手上路
帖 子:174
专家分:0
注 册:2005-2-24
收藏
得分:0 

#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 ();

}


坚持就是胜利>>静心,静思
2006-04-10 10:36
271391233
Rank: 1
等 级:新手上路
帖 子:174
专家分:0
注 册:2005-2-24
收藏
得分:0 
#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 ();

}

坚持就是胜利>>静心,静思
2006-04-10 10:36
271391233
Rank: 1
等 级:新手上路
帖 子:174
专家分:0
注 册:2005-2-24
收藏
得分:0 

#include<iostream.h>
class Caculate
{
private:
int number,count;
public:
int add(int a,int b)
{
number=a;
count=b;
return (a+b);
}
int jian(int a, int b)
{
number=a;
count=b;
return (a-b);
}
int cheng(int a, int b)
{
number=a;
count=b;
return(a*b);
}
int chu(int a, int b)
{
number=a;
count=b;
return(a/b);
}
};
void main()
{
Caculate s;
int a,b;
cout<<"请输入两个数:"<<endl;
cin>>a>>b;
cout<<"两个数的和是:"<<s.add(a,b)<<endl;
cout<<"两个数的差是:"<<s.jian(a,b)<<endl;
cout<<"两个数的积是:"<<s.cheng(a,b)<<endl;
if(b==0)
{
cout<<"除数不能为0"<<endl;
}
else
{
cout<<"两个数相除是:"<<s.chu(a,b)<<endl;
}

}


坚持就是胜利>>静心,静思
2006-04-10 10:37
快速回复:[原创]构造函数
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030440 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved