今天看了类,写下一下笔记,一般的书上的程序都是对一个类的情况而言,我开始也想知道到底可以定义两个或者更加多的类不啊?
编写程序才晓得,答案是肯定的!
还有一点就是,非成员函数不可以访问类里面的私有成员,包括变量和函数,友员函数出外。
一个类里面的成员函数也不可以访问别的类里面的私有成员,因为他们对于别的类而言就是非成员函数。
公有成员是可以访问的。
成员函数的形式参数是可以任意变化的,只要有需要,这个就是函数重载的知识了,相同的类可以相互访问。比如:
Time Time::Sum(const Time t) const
{
Time sum;
sum.minutes = minutes + t.minutes;
sum.hours = hours + t.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
}
Time和t是相同的类定义的两个变量,是可以相互访问的。而:
Time Time::Sum(const Tt t) const
{
Time sum;
sum.minutes = minutes + t.minutes;
sum.hours = hours + t.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
}
Tt是另一个类定义,这样是没有访问权限的。
看一个具体的程序把,两个源文件和一个头文件:
mytime1.h
#ifndef MYTIME1_H_
#define MYTIME1_H_
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h, int m = 0);
void AddMin(int m);
void AddHr(int h);
void Reset(int h = 0, int m = 0);
Time Sum(const Time t) const;
void Show() const;
};
class Tt
{
private:
int hours;
int minutes;
public:
Tt();
Tt(int h, int m = 0);
void AddMin(int m);
void AddHr(int h);
void Reset(int h = 0, int m = 0);
Tt Sum( Tt t) const;
void Show() const;
};
#endif
mytime.cpp
// mytime0.cpp -- implementing Time methods
#include <iostream>
#include "mytime1.h"
Time::Time()
{
hours = minutes = 0;
}
Time::Time(int h ,int m)
{
hours = h;
minutes = m;
}
void Time::AddMin(int m)
{
minutes += m;
hours += minutes / 60;
minutes %= 60;
}
void Time::AddHr(int h)
{
hours += h;
}
void Time::Reset(int h, int m)
{
hours = h;
minutes = m;
}
Time Time::Sum(const Time t) const
{
Time sum;
sum.minutes = minutes + t.minutes;
sum.hours = hours + t.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
}
void Time::Show() const
{
std::cout << hours << " hours, " << minutes << " minutes";
}
Tt::Tt()
{
hours = minutes = 0;
}
Tt::Tt(int h ,int m)
{
hours = h;
minutes = m;
}
void Tt::AddMin(int m)
{
minutes += m;
hours += minutes / 60;
minutes %= 60;
}
void Tt::AddHr(int h)
{
hours += h;
}
void Tt::Reset(int h, int m)
{
hours = h;
minutes = m;
}
Tt Tt::Sum(Tt t) const
{
Tt sum;
sum.minutes = minutes + t.minutes;
sum.hours = hours + t.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
}
void Tt::Show() const
{
std::cout << hours << " hours, " << minutes << " minutes";
}
main.cpp
#include <iostream>
#include "mytime1.h"
int main()
{
using std::cout;
using std::endl;
Time planning;
Time coding(2,40);
Time fixing(5,32);
Time total;
cout << "planning time = ";
planning.Show();
cout << endl;
cout << "coding time = ";
coding.Show();
cout<<endl;
coding.AddMin(50);
coding.Show();
cout << endl;
cout << "fixing time = ";
fixing.Show();
cout << endl;
total = coding.Sum(fixing);
cout << "coding.Sum(fixing) = ";
total.Show();
cout << endl;
Tt plannin;
Tt codin(2,40);
Tt fixin(5,32);
Tt tota;
cout << "planning tt = ";
plannin.Show();
cout << endl;
cout << "coding time = ";
codin.Show();
cout<<endl;
codin.AddMin(50);
codin.Show();
cout << endl;
cout << "fixing time = ";
fixin.Show();
cout << endl;
tota = codin.Sum(fixin);
cout << "codin.Sum(fixin) = ";
tota.Show();
cout << endl;
return 0;
}