资源描述
1.1
编写一个基于对象的程序,规定:
(1)定义一个时间类Time,类内有私有数据成员hour(小时)、minute(分钟)、sec(秒),公有成员函数set_time()、show_time()。
(2)set_time()函数和show_time()函数在类内定义。set_time()作用是从键盘输入时间、分钟、秒的值,show_time()的作用是在屏幕上显示时间、分钟、秒的值。
(3)在main()函数定义Time类的对象t1,并调用set_time()函数给时间赋值,调用show_time()函数输出时间的值。
#include <iostream>
using namespace std;
class Time
{public:
void set_time()
{cin>>hour;
cin>>minute;
cin>>sec;
}
void show_time()
{cout<<hour<<":"<<minute<<":"<<sec<<endl;}
private:
int hour;
int minute;
int sec;
};
int main()
{
Time t1;
t1.set_time();
t1.show_time();
return 0;
}
1.2
编写一个基于对象的程序,求长方体的体积,规定:
(1)定义一个长方体类Box,类内有私有数据成员lengh(长)、width(宽)、height(高),公有成员函数get_value()、volume()。
(2)get_value()函数和volume()函数在类外定义。get_value()作用是从键盘输入长、宽、高的值,volume()的作用是计算长方体的体积并在屏幕上显示。
(3)在main()函数定义Box类的对象box1,并调用get_value()函数给长、宽、高赋值,调用volume()函数输出长方体体积。
#include <iostream>
using namespace std;
class Box
{public:
void get_value();
void volume();
private:
float lengh;
float width;
float height;
};
void Box::get_value()
{ cout<<"please input lengh, width,height:";
cin>>lengh;
cin>>width;
cin>>height;
}
void Box::volume()
{ cout<<"volmue of box1 is "<<lengh*width*height<<endl;}
int main()
{Box box1;
box1.get_value();
box1.volume();
return 0;
}
1.3.
编写一个基于对象的程序,求一个有十个数据的整型数组中元素的最大值,规定:
(1)定义一个类Array_max,类内有私有数据成员array[10]、max分别存储十个整数、最大值,公有成员函数set_value()、max_volume()。
(2)set_value()函数和max_volume()函数在类外定义。get_value()作用是从键盘输入数组十个元素的值,max_volume()的作用是求出并显示数组元素的最大值。
(3)在main()函数定义Array_max类的对象arrmax,并调用set_value()函数给数组赋值,调用max_volume()函数求出并显示数组元素的最大值。
#include <iostream>
using namespace std;
class Array_max
{public:
void set_value();
void max_value();
private:
int array[10];
int max;
};
void Array_max::set_value()
{ int i;
for (i=0;i<10;i++)
cin>>array[i];
}
void Array_max::max_value()
{int i;
max=array[0];
for (i=1;i<10;i++)
if(array[i]>max) max=array[i];
cout<<"max="<<max;
}
int main()
{Array_max arrmax;
arrmax.set_value();
arrmax.max_value();
return 0;
}
1.4
编写一个程序,用成员函数重载运算符“+”,使之能用于两个复数相加。
#include <iostream>
using namespace std;
class Complex
{public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex operator + (Complex &c2);
void display();
private:
double real;
double imag;
};
Complex Complex::operator + (Complex &c2)
{Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;}
void Complex::display()
{cout<<"("<<real<<","<<imag<<"i)"<<endl;}
int main()
{Complex c1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
cout<<"c1+c2=";c3.display();
return 0;
}
1.5
编写一个程序,用友元函数重载运算符“+”,使之能用于两个复数相加。
#include <iostream.h>
class Complex
{public:
Complex(){real=0;imag=0;}
Complex(double r){real=r;imag=0;}
Complex(double r,double i){real=r;imag=i;}
friend Complex operator+ (Complex &c1,Complex &c2);
void display();
private:
double real;
double imag;
};
Complex operator+ (Complex &c1,Complex &c2)
{return Complex(c1.real+c2.real, c1.imag+c2.imag);}
void Complex::display()
{cout<<"("<<real<<","<<imag<<"i)"<<endl;}
int main()
{Complex c1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c1="; c1.display();
cout<<"c2="; c2.display();
cout<<"c1+c2="; c3.display();
return 0;
}
1.6
编写一个基于对象的程序,求圆球的体积,规定:
(1)定义一个圆球类Circle,类内有私有数据成员radius(半径),公有成员函数get_value()、volume()。
(2)get_value()函数和volume()函数在类外定义。get_value()作用是从键盘输入半径的值,volume()的作用是计算圆球的体积并在屏幕上显示。
(圆球体积计算公式为:v=4/3πr3)
(3)在main()函数定义Circle类的对象circle1,并调用get_value()函数给球半径赋值,调用volume()函数输出圆球的体积。
#include <iostream>
using namespace std;
class Circle
{public:
void get_value();
void volume();
private:
float radius;
};
void Circle::get_value()
{ cout<<"please input radius:";
cin>>radius;
}
void Circle::volume()
{ cout<<"volmue of circle1 is "<<4.0/3*3.14159*radius*radius*radius<<endl;}
int main()
{Circle circle1;
circle1.get_value();
circle1.volume();
return 0;
}
1.7
编写一个基于对象的程序,规定:
(1)定义一个日期类Date,类内有私有数据成员year(年)、month(月)、day(日),公有成员函数set_date()、show_date()。
(2)set_date()函数和show_date()函数在类外定义。set_date()作用是从键盘输入年、月、日的值,show_date()的作用是在屏幕上显示年、月、日的值。
(3)在main()函数定义Date类的对象d1,并调用set_ date()函数给日期赋值,调用show_date()函数输出日期的值。
#include <iostream>
using namespace std;
class Date
{public:
void set_date();
void show_date();
private:
int year;
int month;
int day;
};
void Date::set_date()
{cin>>year;
cin>>month;
cin>>day;
}
void Date::show_date()
{cout<<year<<"-"<<month<<"-"<<day<<endl;}
int main()
{
Date d1;
d1.set_date();
d1.show_date();
return 0;
}
2.1
编写一个面向对象的程序,规定:
(1)定义一个基类Student,类内有私有数据成员num(学号)、name(姓名)、sex(性别),公有成员函数get_value()、display(),get_value()作用是从键盘给num、name、sex赋值,display()的作用是显示num、name、sex的值。
(2)定义一个派生类Student1,Student1公有继承自Student类。Student1类新增私有数据成员age(年龄)、addr(地址),新增公有成员函数get_value_1()、display_1()。get_value_1()的作用是实现从键盘给num、name、sex、age、addr赋值,display_1()的作用是显示num、name、sex、age、addr的值。
(3)在main()函数定义Student1类的对象stud1,并调用get_value_1()函数给对象赋值,调用display_1()函数显示学生的所有信息。
#include <iostream>
using namespace std;
class Student
{public:
void get_value()
{cin>>num>>name>>sex;}
void display( )
{cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;}
private :
int num;
char name[10];
char sex;
};
class Student1: public Student
{public:
void get_value_1()
{get_value();
cin>>age>>addr;}
void display_1()
{ display();
cout<<"age: "<<age<<endl;
cout<<"address: "<<addr<<endl;}
private:
int age;
char addr[30];
};
int main()
{Student1 stud1;
stud1.get_value_1();
stud1.display_1();
return 0;
}
2.2
编写一个面向对象的程序,规定:
(1)定义一个基类Student,类内有私有数据成员num(学号)、name(姓名)、sex(性别),公有成员函数get_value()、display(),get_value()作用是从键盘给num、name、sex赋值,display()的作用是显示num、name、sex的值。
(2)定义一个派生类Student1,Student1私有继承自Student类。Student1类新增私有数据成员age(年龄)、addr(地址),新增公有成员函数get_value_1()、display_1()。get_value_1()的作用是实现从键盘给num、name、sex、age、addr赋值,display_1()的作用是显示num、name、sex、age、addr的值。
(3)在main()函数定义Student1类的对象stud1,并调用get_value_1()函数给对象赋值,调用display_1()函数显示学生的所有信息。
#include <iostream>
using namespace std;
class Student
{public:
void get_value()
{cin>>num>>name>>sex;}
void display( )
{cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;}
private :
int num;
char name[10];
char sex;
};
class Student1: private Student
{public:
void get_value_1()
{get_value();
cin>>age>>addr;}
void display_1()
{display();
cout<<"age: "<<age<<endl;
cout<<"address: "<<addr<<endl;}
private:
int age;
char addr[30];
};
int main()
{Student1 stud1;
stud1.get_value_1();
stud1.display_1();
return 0;
}
2.3
编写一个面向对象的程序,规定:
(1)定义一个基类Student,类内有私有数据成员num(学号)、name(姓名)、sex(性别),公有成员函数get_value()、display(),get_value()作用是从键盘给num、name、sex赋值,display()的作用是显示num、name、sex的值。
(2)定义一个派生类Student1,Student1保护继承自Student类。Student1类新增私有数据成员age(年龄)、addr(地址),新增公有成员函数get_value_1()、display_1()。get_value_1()的作用是实现从键盘给num、name、sex、age、addr赋值,display_1()的作用是显示num、name、sex、age、addr的值。
(3)在main()函数定义Student1类的对象stud1,并调用get_value_1()函数给对象赋值,调用display_1()函数显示学生的所有信息。
#include <iostream>
using namespace std;
class Student
{public:
void get_value()
{cin>>num>>name>>sex;}
void display( )
{cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;}
private :
int num;
char name[10];
char sex;
};
class Student1: protected Student
{public:
void get_value_1()
{get_value();
cin>>age>>addr;}
void display_1()
{display();
cout<<"age: "<<age<<endl;
cout<<"address: "<<addr<<endl;}
private:
int age;
char addr[30];
};
int main()
{Student1 stud1;
stud1.get_value_1();
stud1.display_1();
return 0;
}
2.4
编写一个面向对象的程序,规定:
(1)定义一个基类Student,类内有保护数据成员num(学号)、name(姓名)、sex(性别),公有成员涉及构造函数、show()函数。构造函数带3个参数用于定义对象时赋初值,show()函数作用是显示学生信息,即num、name、sex的值。
(2)定义一个派生类Student1,Student1公有继承自Student类。Student1类新增私有数据成员age(年龄)、addr(地址),新增公有成员涉及构造函数、show()函数。构造函数带5个参数用于定义对象时赋初值,show()函数作用是显示学生信息,即num、name、sex、age、addr的值。
(3)在main()函数定义Student1类的对象stud1并赋初值,调用show()函数显示该学生的所有信息。
#include <iostream>
#include<string>
using namespace std;
class Student
{public:
Student(int n,string nam,char s )
{num=n;
name=nam;
sex=s; }
void show( )
{ cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;
}
protected:
int num;
string name;
char sex ;
};
class Student1: public Student
{public:
Student1(int n,string nam,char s,int a,char ad[]) : Student(n,nam,s)
{age=a;
addr=ad;
}
void show( )
{ Student::show();
cout<<"age: "<<age<<endl;
cout<<"address: "<<addr<<endl<<endl;
}
private:
int age;
string addr;
};
int main( )
{Student1 stud1(10010,"Wang-li",'f',19,"115 Beijing Road,Shanghai");
stud1.show( );
return 0;
}
2.5
编写一个面向对象的程序,规定:
(1)定义一个基类Student,类内有保护数据成员num(学号)、name(姓名),公有成员涉及构造函数、show()函数。构造函数带2个参数用于定义对象时赋初值,show()函数作用是显示学生信息,即num、name的值。
(2)定义一个派生类Student1,Student1公有继承自Student类。Student1类新增私有数据成员age(年龄)、addr(地址)以及子对象monitor(班长,Student类型),新增公有成员涉及构造函数、show()函数。构造函数带6个参数用于定义对象时赋初值,show()函数作用是显示学生的所有信息,即本人的num、name、age、addr以及班长的num、name。
(3)在main()函数定义Student1类的对象stud1并赋初值,调用show()函数显示该学生的所有信息。
#include <iostream>
#include<string>
using namespace std;
class Student
{public:
Student(int n,string nam)
{num=n;
name=nam;
}
void show( )
{ cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
}
protected:
int num;
string name;
};
class Student1: public Student
{public:
Student1(int n,string nam,int n1,string nam1,int a,string ad)
:Student(n,nam),monitor(n1,nam1)
{age=a;
addr=ad;
}
void show( )
{ cout<<"This student is:"<<endl;
Student::show();
cout<<"age: "<<age<<endl;
cout<<"address: "<<addr<<endl<<endl;
cout<<"Class monitor is:"<<endl;
monitor.show();
}
private:
Student monitor;
int age;
string addr;
};
int main( )
{Student1 stud1(10010,"Wang-li",10001,"Li-sun",19,"115 Beijing Road,Shanghai");
stud1.show( );
return 0;
}
2.6
写一个面向对象的程序,定义抽象基类Shape,由它派生出2个类:Circle(圆形)、Rectangle(矩形),显示两个图形的面积。规定:
(1)抽象基类Shape的公有成员有纯虚函数area()。
(2)Circle类公有继承自Shape类,新增数据成员radius(半径),公有成员有构造函数和求圆面积的area()函数。
(3)Rectangle类公有继承自Shape类,新增数据成员length(长)、width(宽),公有成员有构造函数和求矩形面积的area()函数。
(4)在main()函数定义Circle类的对象circle1并赋初值,调用area()函数显示该圆面积;定义Rectangle类的对象rectangle1并赋初值,调用area()函数显示该矩形面积。
#include <iostream>
using namespace std;
class Shape
{public:
virtual double area() const =0;
};
class Circle:public Shape
{public:
Circle(double r):radius(r){}
virtual double area() const {return 3.14159*radius*radius;};
protected:
double radius;
};
class Rectangle:public Shape
{public:
Rectangle(double l,double w):length(l),width(w){}
virtual double area() const {return length*width;}
protected:
double length,width;
};
int main()
{
Circle circle(2.5);
cout<<"area of circle ="<<circle.area()<<endl;
Rectangle rectangle(2,4);
cout<<"area of rectangle ="<<rectangle.area()<<endl;
return 0;
}
2.7
写一个面向对象的程序,定义抽象基类Shape,由它派生出2个类:Square(正方形)、Triangle(三角形),显示两个图形的面积。规定:
(1)抽象基类Shape的公有成员有纯虚函数area()。
(2)Square类公有继承自Shape类,新增数据成员side(边长),公有成员有构造函数和求正方形积的area()函数。
(3)Triangle类公有继承自Shape类,新增数据成员side(边长)、height(高),公有成员有构造函数和求三角形面积的area()函数。
(4)在main()函数定义Square类的对象square1并赋初值,调用area()函数显示该正方形面积;定义Triangle类的对象triangle1并赋初值,调用area()函数显示该三角形面积。
#include <iostream>
using namespace std;
class Shape
{public:
virtual double area() const =0;
};
class Square:public Shape
{public:
Square(double s):side(s){}
virtual double area() const {return side*side;}
protected:
double side;
};
class Triangle:public Shape
{public:
Triangle(double s,double h):side(s),height(h){}
virtual double area() const {return 0.5*side*height;}
protected:
double side,height;
};
int main()
{
Square square1(2.5);
cout<<"area of square ="<<square1.area()<<endl;
Triangle triangle1(2,3);
cout<<"area of triangle ="<<triangle1.area()<<endl;
return 0;
}
庄子云:“人生天地之间,若光阴似箭,忽然而已。”是呀,春秋置换,日月交替,这从指尖悄然划过的时光,没有一点声响,没有一刻停留,仿佛眨眼的功夫,半生已过。
人活在世上,就像暂时寄宿于尘世,当生命的列车驶到终点,情愿也罢,不情愿也罢,微笑也罢,苦笑也罢,都不得不向生命挥手作别。
我们无法挽住时光的脚步,无法改变人生的宿命。但我们可以拿起生活的画笔,把自己的人生涂抹成色彩靓丽的颜色。
生命如此短暂,岂容随意挥霍!只有在该辛勤耕耘的时候播洒汗水,一程风雨后,人生的筐篓里才干装满硕果。
就算是烟花划过天空,也要留下短暂的绚烂。只有让这仅有一次的生命丰盈充实,才不枉来尘世走一遭。雁过留声,人过留名,这一趟人生路程,总该留下点儿什么!
生活是柴米油盐的平淡,也是行色急忙的奔波。一粥一饭来之不易,一丝一缕物力维艰。
前行的路上,有风也有雨。有时候,风雨扑面而来,打在脸上,很疼,可是,我们不能向生活低头认输,咬牙抹去脸上的雨水,尚有泪水,甩开脚步,接着向前。
我们需要呈现最佳的自己给世界,需要许诺最佳的生活给家人。所以,生活再累,不能后退。即使生活赐予我们一杯不加糖的苦咖啡,皱一皱眉头,也要饮下。
人生是一场跋涉,也是一场选择。我们能到达哪里,能看到什么样的风景,能成为什么样的人,都在于我们的选择。
假如我们选择面朝大海,朝着阳光的方向挥手微笑,我们的世界必会收获一片春暖花开。假如我们选择小桥流水,在不动声色的日子里种篱修菊,我们的世界必会收获一隅静谧恬淡。
选择临风起舞,我们就是岁月的勇者;选择临阵脱逃,我们就是生活的懦夫。
没有淌但是去的河,就看我们如何摆渡。没有爬但是去的山,就看我们何时启程。
德国哲学家尼采说:“每一个不曾起舞的日子,都是对生命的辜负。”让我们打开朝着晨光的那扇窗,迎阳光进来,在每一个日出东海的日子,无论是鲜衣怒马少年时,还是宠辱不惊中年时,都活出自己的明媚和精彩。
时间会带来惊喜,只要我们不忘掉为什么出发,不忘掉以梦为马,岁月一定会对我们和颜悦色,前方也一定会故意想不到的惊喜。
人生忽如寄,生活多苦辛。
短暂的生命路程,
别辜负时光,别辜负自己。
愿我们每一个人自律、阳光、勤奋,
活成自己喜欢的模样,
活成一束光,
展开阅读全文