Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 8

///////////////////////////////////////////////////////////////////////////////////

////////////////////////
专题 6.1
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;

class Student
{
public:
void study();
void exam();
string name;
int age;
private:

};

void Student::study()
{
cout <<"学习 C++" <<endl;
}

void Student::exam()
{
cout << "C++考试 100 分" << endl;
}

int main()
{
Student stu;
stu.name = "张三";
stu.age = 20;
cout<< stu.name<<" "<< stu.age << " " <<"岁" << endl;
stu.study();
stu.exam();
printf("名字+学号");
return 0;
}

///////////////////////////////////////////////////////////////////////////////////
////////////////////////
专题 6.2
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;

class Student
{
public:
void study();
void exam();
void setName(string name);
void setAge(int age);
string getName();
int getAge();
private:
string _name;
int _age;
};

void Student::study()
{
cout << "学习 C++" << endl;
}
void Student::exam()
{
cout << "C++考试 100 分" << endl;
}
void Student::setName(string name)
{
_name = name;
}
void Student::setAge(int age)
{
if (age < 0 || age>100)
{
cout << "_name"<<"年龄输入错误" << endl;
_age = 0;
}
else
{
_age = age;
}
}

string Student::getName()
{
return _name;
}

int Student::getAge()
{
return _age;
}

int main()
{
Student stu;
stu.setName("张三");
stu.setAge(20);
cout << stu.getName() << stu.getAge() << "岁" << endl;
stu.study();
stu.exam();
Student stu1;
stu1.setName("李四");
stu1.setAge(22);
cout << stu1.getName() << stu1.getAge() << "岁" << endl;
stu1.study();
stu1.exam();
printf("名字+学号");
return 0;
}
///////////////////////////////////////////////////////////////////////////////////
////////////////////////
专题 6.3

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;

class Clock
{
public:
Clock();
void showTime();

private:
int _hour;
int _min;
int _sec;

};

Clock::Clock()
{
_hour = 0;
_min = 0;
_sec = 0;
}

void Clock::showTime()
{
cout << setw(2) << setfill('0') << _hour << ":"
<< setw(2) << setfill('0') << _min << ":"
<< setw(2) << setfill('0') << _sec << endl;
}

int main()
{
Clock clock;
cout << "clock: ";
clock.showTime();
printf("名字+学号");
return 0;
}

///////////////////////////////////////////////////////////////////////////////////
////////////////////////
专题 6.4
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;

class Clock
{
public:
Clock(int hour,int min,int sec);
void showTime();

private:
int _hour;
int _min;
int _sec;

};

Clock::Clock(int hour, int min, int sec)


{
_hour = hour;
_min = min;
_sec = sec;
}

void Clock::showTime()
{
cout << setw(2) << setfill('0') << _hour << ":"
<< setw(2) << setfill('0') << _min << ":"
<< setw(2) << setfill('0') << _sec << endl;
}

int main()
{
Clock clock1(10,20,30);
cout << "clock1: ";
clock1.showTime();

Clock clock2(22,16,12);
cout << "clock2: ";
clock2.showTime();

printf("名字+学号");
return 0;
}
///////////////////////////////////////////////////////////////////////////////////
////////////////////////
专题 6.5

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;

class Birth {
public:
Birth(int year, int month, int day); void show();
private:
int _year; int _month; int _day;
};
Birth::Birth(int year, int month, int day)
:_year(year), _month(month), _day(day)
{
cout << "Birth 类构造函数" << endl;
}
void Birth::show()
{
cout << "出生日期:" << _year << "-" << _month << "-" << _day << endl;
}
class Student
{
public:
Student(string name, int id, int year, int month, int day);
void show();
private:
string _name;
int _id;
Birth birth;
};
Student::Student(string name, int id, int year, int month, int day)
:birth(year, month, day)
{
cout << "student 类构造函数" << endl;
_name = name;
_id = id;
}
void Student::show()
{
cout << "姓名:" << _name << endl;
cout << "学号:" << _id << endl;
birth.show();
}
int main()
{
Student stu("lili", 210002, 2000, 1, 1);
stu.show();
printf("名字+学号");
return 0;
}

///////////////////////////////////////////////////////////////////////////////////
////////////////////////
专题 6.6

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;

class rabbit
{
public:
rabbit(string name,const char *pf);
void eat();
~rabbit();

private:
string _name;
char *_food;
};

rabbit::rabbit(string name, const char *pf)


{
cout << "调用构造函数" << endl;
_name = name;
_food = new char[50];
memset(_food,0,50);
strcpy(_food,pf);
}

void rabbit::eat()
{
cout<<_name << " is eating "<< _food << endl;
}

rabbit::~rabbit()
{
cout << "调用析构函数,析构"<<_name << endl;
if (_food != null)
delete []_food;
}
int main()
{
rabbit a("a","luobo");
a.eat();
rabbit b("b","baicai");
b.eat();

printf("名字+学号");
return 0;
}

///////////////////////////////////////////////////////////////////////////////////
////////////////////////
专题 6.7

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;

class Student
{
public:
Student(string name);
~Student();
static int _sum;
private:
string _name;
};

Student::Student(string name)
{
this->_name = name;
_sum++;
}

Student::~Student()
{
}

int Student::_sum = 0;
int main()
{
Student stu1("张三");
cout << "人数是:" << stu1._sum << endl;
Student stu2("李四");
cout << "人数是:"<< stu2._sum << endl;

cout << " 人数是:" << Student::_sum << endl;


cout << "stu1 的大小是:" << sizeof(stu1)<< endl;
printf("名字+学号");
return 0;
}

///////////////////////////////////////////////////////////////////////////////////
////////////////////////
专题 6.8

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;

class Point
{
public:
Point(float x,float y);
~Point();
static float getLen(Point &p1, Point &p2);
static float _len;
private:
float _x;
float _y;
};

float Point::_len = 0;

Point::Point(float x, float y):_x(x),_y(y)


{
cout << "初始化坐标点" << endl;
}
Point::~Point()
{
}
float Point::getLen(Point &p1, Point &p2)
{
float x = abs(p1._x - p2._x);
float y = abs(p1._y - p2._y);
_len = sqrtf(x*x + y * y);
return _len;
}

int main()
{
Point p1(1, 2);
Point p2(6, 8);
cout << Point::getLen(p1, p2) << endl;
printf("名字+学号");
return 0;
}

You might also like