//#define CLASS_STUDENT
//#define CLASS_TIME1_PUBLIC_MEMBER
//#define CLASS_TIME2_OUT_FUN
//#define CLASS_TIME3_NO_PARAM_CONSTUCT //没有构造函数+没有参数构造函数
//#define CLASS_TIME4_POINTER //对象指针
//#define CLASS_FIND_MAX
//#define CONSTRUCTOR_DECONSTRUCTOT //构造函数,析构函数,对象数组
//#define CLASS_STATIC_FUN //静态成员函数--静态成员
//#define GENERNAL_AND_MEMBER_FRIEND_FUN //普通函数声明为友元函数,和其他成员函数声明为友元函数
#define CLASS_TEMPLATE //类模板
/
/*
主函数精简,只是调用对象的成员函数,完成相关操作。大多数情况下,主函数甚至不出现控制类的结构,而在成员函数中使用控制结构。
面向对象的程序设计中,最关键的工作是类的设计,所有的数据和数据的操作都是在类中完成。只要把类定义好,编程工作就简单了。
*/
/*const
1、常对象:数据成员必须有初值; time const t1(11,12);只能通过构造函数的初始化表进行初始化,其值不可改变,但是每个对象的数据成员值可以不同.
2、常成员函数:可以访问对象常成员; void getTime()const;
常成员函数可以引用常数据成员和普通数据成员;外界不能调用一个常对象的非const型的成员函数.
3、常数据成员可以被常成员函数和普通成员函数访问,但不可修改。
4、指向对象的常指针:指针的指向始终不变; time *const p=&t1; p 始终指向t1不能改变,但是t1里面的数据成员可以改变.
5、指向常对象的指变:const time*p; time const* p;p指向的是常对象,*p的对象数据成员不能被改变,但是p可以改变指向,指向另一个常对象。
一个常对象,只能通过指向常对象的指变来指向它。
一个指向常对象的指变可以访问非const对象,但不能改变。
6、常引用:void fun(const & time& t);函数中不能修改形参-引用;---常指针和常引用作为函参,既能保证数据安全,调用函数时也不必建立实参拷贝。
*/
/
#ifdef CLASS_TEMPLATE
#include <iostream>
using namespace std;
//由来:多个类功能相同,只是数据类型不同,可以声明通用的类模板,里面包含虚拟类型参数.
template <class numtype> //第一步: 定义类模板 template <class numtype> //numtype是数据类型名
// template <class T,class T1>
class compare //多个类公用类名
{
public:
compare(numtype a, numtype b) //定义构造函数--第二步:所有类型名用虚拟类型名替代
{
x=a;
y=b;
}
numtype max()
{
return (x>y)?x:y;
}
numtype min()
{
return (x<y)?x:y;
}
private:
numtype x,y;
};
int main()
{
compare <int>comp1(3,7); //定义对象comp1,用于两个整数的比较。
cout<<comp1.max()<<" is the maxinum of two integer numbers."<<endl;
cout<<comp1.min()<<" is the mininum of two integer numbers."<<endl<<endl;
compare <float>comp2(12.1,34.23); //两个浮点数的比较。
cout<<comp2.max()<<" is the maxinum of two float numbers."<<endl;
cout<<comp2.min()<<" is the mininum of two float numbers."<<endl<<endl;
compare <char>comp3('a','A'); //两个字符数的比较。
cout<<comp3.max()<<" is the maxinum of two characters."<<endl;
cout<<comp3.min()<<" is the mininum of two characters."<<endl<<endl;
system("pause");
return 0;
}
#elif defined GENERNAL_AND_MEMBER_FRIEND_FUN
/*
1】this指针:类的不同对象调用成员函数都是调用同一个函数代码段;每个成员函数里面都包含一个隐式的this指针,用来指向调用对象;this也可以显式使用-如QT中.
int box::volume(box *this)
{return (this->height)*(this->width)*(this->length);} //编译器处理
*/
/*
1】友元可以访问与其有好友关系的类中的私有成员,包括友元函数与友元类。
2】友元函数:可以是普通函数和其他类的成员函数,类外声明,在类体内用freid进行声明.
3】友元类:友元类B的所有函数都是A类的友元函数,可以访问A类中的所有成员。
*/
#include <iostream>
using namespace std;
class date; //类的提前声明.
class time
{
public:
time(int, int, int);
void display(date &);
private:
int hour;
int minute;
int sec;
};
class date
{
public:
date(int, int, int);
friend void time::display(date &);
private:
int year;
int month;
int day;
};
time::time(int h, int m, int s)
{
hour = h;
minute = m;
sec = s;
}
void time::display(date &d)
{
cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl;
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
date::date(int m, int d, int y)
{
month = m;
day = d;
year = y;
}
int main()
{
time t1(10, 12, 13);
date d1(06, 19, 2011);
t1.display(d1);
system("pause");
return 0;
}
///
#elif defined CLASS_STATIC_FUN
//静态数据成员的由来: 希望在同类的多个对象之间实现数据共享,对象之间可以沟通,而不用全局变量.
#include <iostream>
using namespace std;
class student
{
public:
student(int n, int a, float s):num(n),age(a),score(s){}
void total();
static float average(); //声明静态成员函数
private:
int num; //学号
int age; //年龄
float score; //成绩
static float sum; //成绩总和 //静态数据成员; 变量一直累加,属于类,不属于对象。
static int count; //求平均分的学生个数 //静态数据成员,值一直变化,但对于对象值是相同的。
};//生存期:不随对象的建立而分配空间,也不随对象的撤销而释放。是在程序编译时分配空间,到程序结束时才释放的。
//求学生成绩总和
void student::total() //公用成员函数也能引用本类中的私有静态成员函数 //养成习惯:只用静态成员函数引用静态数据成员,而不引用非静态数据成员。
{
sum += score;
count++;
}
//求成绩的平均分 //属于类,所以没有this指针.
float student::average() //静态成员函数,主要是为了方便处理静态数据成员(属于类,公用的)。在静态成员函数中访问非静态数据成员,要加对象名以限定。
{
return (sum/count); //此程序的stud[3]被限定在main中,属于局部变量,这个函数里面不能引用它们。要在此函数里面调用其他非静态成员,可以采用全局对象或者使用对象参数。
}
float student::sum = 0; //初始化:静态数据成员只能在类体外初始化,如果未赋值自动为0.
int student::count = 0; //引用:公用成员可以同类名和对象名访问,私有的必须通过公用成员函数引用.
int main()
{
student stud[3] = { //对象数组定义的时候初始化
student(130, 20, 90),
student(131, 21, 91), //student stud[3]{60,70,80}; 构造函数只有一个实参这样初始化.
student(132, 22, 93)
};
int processNum = 0;
cout<<"Please input the number of student:";
cin>>processNum;
for (int i=0; i<processNum; i++)
{
stud[i].total();
}
cout<<"The average score of "<<processNum<<"student is "<<student::average()<<endl;
system("pause");
return 0;
}
#elif defined CONSTRUCTOR_DECONSTRUCTOT
/*
构造函数名字必须与类名相同,建立对象时自动调用,不具有任何类型,没有返回值。用户没有定义,则系统会自动生成一个函数。
析构函数:
1]作用:不是删除对象,而是撤销对象占用内存前的清理工作;不仅用于释放资源,还能执行其他操作。
2]构成:类名前面加~,无返回值,无类型,无参数,不重载。
3]执行时间:对象生命期结束后自动调用;区别局部对象、static局部对象,全局对象,new建立对象的结束时间。
4]默认析构函数
*/
//两个长方形柱体,长宽高[1]12,13,14;[2]15,16,17;分别求它们的体积;编一个基于对象的程序,在类中使用带参的构造函数。
#include <iostream>
using namespace std;
class box
{
public:
//box(); //无参构造函数,也是默认参数构造函数,一个类只能有一个;
box(int h=10, int w=10, int l=10); //带默认参数的构造函数 box(int=10,int=10,int=10);
//构造函数重载,一个无参,一个有参
//box(int, int, int); //构造函数声明,可以只有类型,不带参数。
//box(int h,int w,int l):height(h),width(w),length(l){} //可以这里定义
~box() //析构函数,前面加~; 函数结束时自动调用,按类的定义,顺序调用。
{
cout<<"Destructor called."<<endl;
}
int volume();
private:
int height;
int width;
int length;
};
//返回体积值
int box::volume()
{
return(height*width*length);
}
//无参构造函数=默认构造函数
//box::box()
//{
// height = 10;
// width = 10;
// length = 10;
//}
//box::box(int h=10, int w=10, int l=10):height(h),width(w),length(l) //默认参数构造函数
//box::box(int h,int w,int l):height(h),width(w),length(l){}
box::box(int h, int w, int l)
{
height = h;
width = w;
length = l;
}
int main()
{
box box1(12, 13, 14);
cout<<"The volume of box1 is : "<<box1.volume()<<endl;
box box2(15,16,17);
cout<<"The volume of box2 is : "<<box2.volume()<<endl;
box box3;
cout<<"The volume of box3 is : "<<box3.volume()<<endl;
box box4(18,19);
cout<<"The volume of box4 is : "<<box4.volume()<<endl;
box a[3] =
{
box(10, 11, 12),
box(13, 14, 15),
box(16, 17, 18)
};
cout<<"volume of a[0] is "<<a[0].volume()<<endl;
cout<<"volume of a[1] is "<<a[1].volume()<<endl;
cout<<"volume of a[2] is "<<a[2].volume()<<endl;
//system("pause"); //有这函数,看不到调用析构函数.
return 0;
}
#elif defined CLASS_FIND_MAX
#include <iostream>
using namespace std;
class arrayMax
{
public:
void setValue();
void maxValue();
void showValue();
private:
int array[10];
int m_max;
};
void arrayMax::setValue()
{
for (int i=0; i<10; i++)
{
cin>>array[i];
}
}
void arrayMax::maxValue()
{
m_max = array[0];
for (int i=1; i<10; i++)
{
if (m_max<array[i])
{
m_max = array[i];
}
}
}
void arrayMax::showValue()
{
cout<<"max: "<<m_max<<endl;
}
int main(int argc, char *argv[])
{
arrayMax arrayTest;
arrayTest.setValue();
arrayTest.maxValue();
arrayTest.showValue();
system("pause");
return 0;
}
#elif defined CLASS_TIME4_POINTER
/*
1]函数指变使用: 定义:void (*P)(); 赋值:p=fun; 调用:(*P)();
2]若不用指针而直接用对象名作为形参,函数中可以修改形参的值,但不能改变对应的实参的值。因为对象做函参时,在函数调用时将建立一个新的对象,其是实参对象的拷贝。
实参把值传给形参,二者占不同的存储空间。形参的修改不会影响到实参,而且这种虚实结合形式产生实参的拷贝对时间和空间的开销大。
——引用则只是传递地址,二者指向同一内存。
*/
#include <iostream>
using namespace std;
class Time
{
public:
Time(int, int, int);
void get_time();
int hour;
int minute;
int second;
};
Time::Time(int h, int m, int s)
{
hour = h;
minute = m;
second = s;
}
void Time::get_time()
{
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
int main()
{
Time t1(10, 13, 56);
int *p1 = &t1.hour; //指针指向对象成员。
cout<<"Hour is "<<*p1<<endl;
t1.get_time();
Time *p2 = &t1; //指针指向对象
p2->get_time();
void (Time::*p3)(); //指针指向Time类的公用成员函数=成员函数是属于类的
p3 = &Time::get_time;
(t1.*p3)(); //指针变量需要具体的对象
system("pause");
return 0;
}
#elif defined CLASS_TIME3_NO_PARAM_CONSTUCT
/*
对象的赋值:对象名1=对象名2;
对象的复制: 类名 对象2(对象1); box box2(box1);
复制构造函数; 类名 对象名1=对象名2;
*/
#include <iostream>
using namespace std;
class Time
{
public: //可以不要构造函数 //构造函数作用:对对象中的数据成员赋初值
Time() //无参构造函数:每个对象的成员变量都一样
{
hour = 0; //这里可以是全部函数,也可以是函数声明。
minute = 0;
sec = 0;
cout<<"Time constructor"<<endl;
}
void set_time();
void show_time();
private:
int hour;
int minute;
int sec;
};
void Time::set_time()
{
cin>>hour;
cin>>minute;
cin>>sec;
}
void Time::show_time()
{
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
int main(int argc, char *argv[])
{
Time t1; //建立对象t1,同时调用构造函数t1.time()进行变量赋值。
t1.set_time();
t1.show_time();
Time t2;
t2.set_time();
t2.show_time();
system("pause");
return 0;
}
#elif defined CLASS_TIME2_OUT_FUN
#include <iostream>
using namespace std;
class Time
{
public:
int hour;
int minute;
int sec;
};
int main(int argc, char *grgv[])
{
void set_time(Time &, int hour=0, int minute=0, int sec=0); //声明函数,指定默认形参,靠右边;
void show_time(Time &); //引用做形参
Time t1;
set_time(t1, 12, 23, 24);
show_time(t1);
Time t2;
set_time(t2);
show_time(t2); //默认参数的调用
system("pause");
return 0;
}
void set_time(Time &t, int hour, int minute, int sec)
{
t.hour = hour;
t.minute = minute;
t.sec = sec;
}
void show_time(Time &t)
{
cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;
}
/
#elif defined CLASS_TIME1_PUBLIC_MEMBER
#include <iostream>
using namespace std;
class Time
{
public: //数据成员公用,暴露不安全。声明为公用的成员,既可以被本类中的成员函数引用,也可以被类的作用域内的其他函数引用。
int hour;
int minute;
int sec;
};
int main(int argc, char *argv[])
{
Time t1;
cin>>t1.hour;
cin>>t1.minute;
cin>>t1.sec;
cout<<t1.hour<<":"<<t1.minute<<":"<<t1.sec<<endl;
system("pause");
return 0;
}
//
#elif defined CLASS_STUDENT
//====================student.cpp //一个类一个cpp
#include <iostream>
#include "student.h"
void student::display() //:: 作用域限定符
{
cout<<"num"<<num<<endl;
cout<<"name"<<name<<endl;
cout<<"sex"<<sex<<endl;
}
//====================student.h
#include<string>
using namespace std;
class student
{
public:
void display(); //体内声明,体外定义
private:
int num;
string name;
char sex;
};
//====================
#include <iostream>
#include "student.h"
using namespace std;
int main(int argc, char *argv[])
{
student stud;
stud.display();
system("pause");
return 0;
}
#endif
C++ 03从简单程序引入2
最新推荐文章于 2024-02-19 16:35:53 发布