#if 1
#include <iostream>//静态成员函数的使用
using namespace std;
class Student{
public:
Student(int n,int a,int s):num(n),age(a),score(s){}
void total(Student stu);
static float average(Student stu);//声明静态成员函数
private:
int num;
int age;
float score;
static float sum;
static int count;
};
Student stud[3] = {Student(001,10,100),Student(002,10,99),Student(003,11,101)};
float Student::sum = 0.0;//对公用静态数据成员初始化
int Student::count = 0.0;//对公用静态数据成员初始化
void Student::total(Student stu)
{
cout<<score<<endl;
sum+=score;
count++;
}
float Student::average(Student stu)//函数参数为对象
{
//cout<<stu.score<<endl;静态函数只能访问静态变量
return (sum/count);
}
int main()
{
int n;
cout<<"please input the number of student:";
cin>>n;
for(int i = 0;i<n;i++)
{
stud[i].total(Student stud[i]);/why??????????????????????????
}
cout<<"the average score of "<<n<<"student is"<<Student::average(stud[0])<<endl;
return 0;
}
#endif // 1
==============================上面是错的,下面是对的======================================
#if 1
#include <iostream>//静态成员函数的使用
using namespace std;
class Student{
public:
Student(int n,int a,int s):num(n),age(a),score(s){}
void total();
static float average(Student stu);//声明静态成员函数
private:
int num;
int age;
float score;
static float sum;
static int count;
};
Student stud[3] = {Student(001,10,100),Student(002,10,99),Student(003,11,101)};
float Student::sum = 0.0;//对公用静态数据成员初始化
int Student::count = 0.0;//对公用静态数据成员初始化
void Student::total()//非静态函数访问非静态变量,不用传入对象名,到时候哪个对象调用就指向哪个对象
{
sum+=score;
count++;
}
float Student::average(Student stu)//函数参数为对象
{
cout<<stu.score<<endl;//用 这种特殊的方式实现了静态成员函数访问非静态成员变量 但是应该尽量的保证静态函数只访问静态变量
return (sum/count);
}
int main()
{
int n;
cout<<"please input the number of student:";
cin>>n;
for(int i = 0;i<n;i++)
{
stud[i].total();/why??????????????????????????
}
cout<<"the average score of "<<n<<"student is"<<Student::average(stud[0])<<endl;
return 0;
}
#endif // 1
===================================================================================================
#if 0
#include <iostream>
using namespace std;
class Time
{
public:
Time(int ,int ,int);
friend void display(Time &);
private:
int hour;
int minute;
int sec;
};
Time::Time(int h,int m,int s)
{
hour = h;
minute = m;
sec = s;
}
/*void display(Time &t)
{
cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;//display函数不是Time类的成员函数,不能默认引用Time类的数据成员,必须制定访问的对象,不能写成cout<<hour<<minute
}*/
int main()
{
Time t1(10,13,56);
display(t1);//
return 0;
}
#endif
#if 0
#include <iostream>
using namespace std;
class Date;
class Time{
public:
Time(int ,int,int );
friend void display(Time &);
private:
int hour;
int minute;
int sec;
};
class Date
{
public:
Date(int ,int,int);
friend void display(Date &);//time 类的成员函数是本类的友元函数,
private:
int year;
int month;
int date;
};
Time::Time(int h,int m,int s )
{
hour = h;
minute = m;
sec = s;
}
void display(Time &t)
{
cout<<t.hour<<":"<<t.minute<<":"<<t.sec;//引用本类对象中的成员不用加对象名称
}
void display(Date &d)
{
cout<<d.date<<":"<<d.month<<":"<<d.year;
}
Date::Date(int d ,int m ,int y )
{
date = d;
month = m;
year= y;
}
int main()
{
Time t1(10,13,56);
Date d1(12,25,2004);
display(t1);
display(d1);
return 0;
}
#endif // 1
#if 0
#include <iostream>//对象数组的使用
using namespace std;
class Box
{
public:
Box(int h = 10,int w = 12,int len = 15):height(h),width(w),length(len){}//声明有默认参数的构造函数,用参数初始化表对数据成员初始化
int volume();
private:
int height;
int width;
int length;
};
int Box::volume()
{
return(height*width*length);
}
int main()
{
Box a[3] = {Box(10,12,15),Box(15,18,20),Box(16,20,26)};//对象数组元素之间用逗号分隔开
cout<<a[0].volume()<<endl;
}
#endif // 0
#if 0
#include <iostream>//静态成员函数的使用
using namespace std;
class Student{
public:
Student(int n,int a,int s):num(n),age(a),score(s){}
void total();
static float average(Student stu);//声明静态成员函数
private:
int num;
int age;
float score;
static float sum;
static int count;
};
Student stud[3] = {Student(001,10,100),Student(002,10,99),Student(003,11,101)};
float Student::sum = 0.0;//对公用静态数据成员初始化
int Student::count = 0.0;//对公用静态数据成员初始化
void Student::total()//非静态函数访问非静态变量,不用传入对象名,到时候哪个对象调用就指向哪个对象
{
sum+=score;
count++;
}
float Student::average(Student stu)//函数参数为对象
{
cout<<stu.score<<endl;//用 这种特殊的方式实现了静态成员函数访问非静态成员变量 但是应该尽量的保证静态函数只访问静态变量
return (sum/count);
}
int main()
{
int n;
cout<<"please input the number of student:";
cin>>n;
for(int i = 0;i<n;i++)
{
stud[i].total();/why??????????????????????????
}
cout<<"the average score of "<<n<<"student is"<<Student::average(stud[0])<<endl;
return 0;
}
#endif // 1
#if 0
#include <iostream>//声明类模板,实现整数,浮点数,字符的比较,求出大数和小数,,,类模板的成员函数在类内实现
using namespace std;
template<class numtype>
class Compare
{
public:
Compare(numtype a,numtype b)
{
x = a;y = b;
}
numtype max()
{
return (x>y)?x:y;
}
numtype min()
{
return (x>y)?y:x;
}
private:
numtype x,y;
};
int main()
{
Compare<double > di(1.5,1.2);
Compare<int > ii(1,2);
cout<<"max"<<di.max()<<"min:"<<di.min() <<endl;
cout<<"max"<<ii.max()<<"min:"<<ii.min();
return 0;
}
#endif // 0
#if 0
#include <iostream>//声明类模板,实现整数,浮点数,字符的比较,求出大数和小数,,,类模板的成员函数在类外实现
using namespace std;
template<class numtype>
class Compare
{
public:
Compare(numtype a,numtype b)
{
x = a;y = b;
}
numtype max();
numtype min();
private:
numtype x,y;
};
template<class numtype>//先把定义为类模板的一句套路写上
numtype Compare<numtype>::max()
{
return (x>y)?x:y;
}
template<class numtype>//先把定义为类模板的一句套路写上
numtype Compare<numtype>::min()
{
return (x>y)?y:x;
}
int main()
{
Compare<double > di(1.5,1.2);
Compare<int > ii(1,2);
cout<<"max"<<di.max()<<"min:"<<di.min() <<endl;
cout<<"max"<<ii.max()<<"min:"<<ii.min();
return 0;
}
#endif // 0
#if 0
#include <iostream>//对象进行默认参数初始化的方式,能够省略掉默认参数的函数的构造函数
using namespace std;
class Date
{
public:
Date(int = 1 ,int = 10,int = 2005 );//Date(int ,int ,int );如果采用这种方式,还要对其他的默认参数废话一遍
//Date();//采用默认参数进行初始化
//Date(int,int);
//Date(int);
void display();
private:
int month;
int day;
int year;
};
Date::Date(int m,int d,int y):month(m),day(d),year(y)
{
}
/*Date::Date(int m,int d):month(m),day(d)
{
year = 2005;
}
Date::Date(int m):month(m)
{
year = 2005;
day = 1;
}
Date::Date()
{
month = 1;
day = 1;
year = 2005;
}*/
void Date::display()
{
cout<<month<<"/"<<day<<"/"<<year<<endl;
}
int main()
{
Date d1(10,13,2005);
Date d2(10,13);
Date d3(10);
Date d4;
d1.display();
d2.display();
d3.display();
d4.display();
return 0;
}
#endif // 1
#if 1//对象数组,元素内容进行输出
#include<iostream>
using namespace std;
class Student
{
private:
int num;
float score;
public:
Student(int n,int s);
void print();
};
Student::Student(int n,int s)
{
num = n;
score = s;
}
void Student::print(){
cout<<num<<score<<endl;
}
int main()
{
Student stud[5] = {Student(01 ,10),Student(02,9),Student(03,11),Student(04,8),Student(05,12)};
Student *p;//指针指向数组首地址
p = stud;
for(int i = 0;i<5;i++)
(*p++).print();
return 0;
}
#endif // 0
///写max函数,将成绩的最大输出,用指针进行遍历,运算符重载
c++ 程序的一点疑问 好像是非静态函数访问了非静态变量
最新推荐文章于 2022-09-27 22:13:58 发布