A 计算圆面积
题目描述
编写一个圆类Circle,实现半径的输入、面积的计算和输出。Circle类的定义如下所示:
class Circle
{
private:
double r;
public:
void input();
double area() const;
void output() const;
};
要求实现Circle类的3个成员函数,完成输入半径、计算面积和输出面积;并按照题目的输入和输出描述测试Circle类。
输入
输入一行,输入圆的半径(double类型)。
输出
输出一行,输出圆的面积(保留小数点后两位)。
输入样例 1
3
输出样例1
28.27
提示
1、在输出面积前使用如下语句:cout<<setiosflags(ios::fixed)<<setprecision(2);来设置输出格式,需要包含iomanip头文件
2、使用acos(-1.0)来得到圆周率的精确值,可将其设置为常量,比如:const double PI=acos(-1.0),需要包含cmath头文件
答案:
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
const double PI = acos(-1.0);
class Circle
{
private:
double r;
public:
void input();
double area() const;
void output() const;
};
void Circle::input(){
cin>>r;
}
double Circle::area() const{
return PI*r*r;
}
void Circle::output() const{
cout<<setiosflags(ios::fixed)<<setprecision(2)<<area();
}
int main(){
Circle c;
c.input();
c.output();
return 0;
}
重要知识点:
简单类与对象知识。
“::”的使用
B 过道和栅栏的造价
题目描述
答案:
#include<iostream>
using namespace std;
class Rectangle
{
private:
double length; //长
double width; //宽
public:
Rectangle(double Length,double Width);
double Area() const; //获取面积
double Perimeter() const;//获取周长
};
Rectangle::Rectangle(double Length=10.,double Width=5.){
length=Length;
width=Width;
}
double Rectangle::Area() const{
return length*width;
}
double Rectangle::Perimeter() const{
return length*2+width*2;
}
int main(){
double l1,w1;
cin>>l1>>w1;
Rectangle R1(l1,w1);
Rectangle R2(l1+3.0,w1+3.0);
cout<<R2.Perimeter()*50<<endl<<(R2.Area()-R1.Area())*240;
}
重要知识点:
构造函数定义与实现。
C 图书类
答案:
#include<iostream>
#include<cstring>
using namespace std;
class Book
{
private:
char * name; //**书名
char * author; //**作者
int sale; //**销售量
public:
Book(); //**无参构造函数
Book(const char *,const char *, int); //**有参构造函数
Book(const Book &); //**拷贝构造函数
void print() const; //**显示数据
~Book(); //**析构函数
};
Book::Book(){
name = new char[100];
strcpy(name,"No name");
author = new char[100];
strcpy(author,"No author");
sale = 0;
}
Book::Book(const char *n, const char *a, int s) {
name = new char[strlen(n) + 1];
strcpy(name, n);
author = new char[strlen(a) + 1];
strcpy(author, a);
sale = s;
}
Book::Book(const Book & tb){
name = new char[strlen(tb.name) + 1];
strcpy(name, tb.name);
author = new char[strlen(tb.author) + 1];
strcpy(author, tb.author);
sale = tb.sale;
}
Book::~Book(){
delete[] name;
delete[] author;
}
void Book::print() const{
cout<<"Name: "<<name<<'\t'<<"Author: "<<author<<'\t'<<"Sale: "<<sale<<endl;
}
int main(){
char tn[100];
char ta[100];
int ts;
cin.getline(tn,100);
cin.getline(ta,100);
cin>>ts;
if (strcmp(tn,"-1") == 0 && strcmp(ta,"-1") == 0 && ts == -1) {
Book bk;
bk.print();
} else if (strcmp(tn, "0") == 0 && strcmp(ta, "0") == 0 && ts == 0) {
Book bk1;
Book bk2(bk1);
bk2.print();
} else {
Book bk(tn,ta,ts);
bk.print();
}
}
重要知识点:
无参构造函数/有参构造函数/拷贝构造函数;
析构函数(new与delete的使用);
C++中的 < cstring >
D 名单类
答案:
#include<iostream>
using namespace std;
class Date //日期类
{
private:
int Date_year; //年
int Date_month; //月
int Date_day; //日
public:
Date(int year=2000, int month=1, int day=1);
void show() const; //以“年-月-日”格式输出年月日
};
Date::Date(int year,int month,int day)
{
Date_year=year;
Date_month=month;
Date_day=day;
}
void Date::show() const
{
cout<<Date_year<<"-"<<Date_month<<"-"<<Date_day;
}
class Croster //名单类
{
private:
string name;
Date birthday;
public:
Croster();
Croster(string name,int year,int month,int day);
Croster(string name, Date date);
void show() const;//显示姓名和出生日期
};
Croster::Croster():name("NULL"),birthday(0,0,0)
{
}
Croster::Croster(string name,int year,int month,int day):birthday(year,month,day)
{
this->name=name;
}
Croster::Croster(string name,Date date):name(name),birthday(date)
{
}
void Croster::show() const
{
cout<<"Name: "<<name<<", "<<"Birthday: ";
birthday.show();
}
int main()
{
int cmd=0;
while(1){
cin>>cmd;
if(cmd==0){
Croster c0;
c0.show();
}else if(cmd==1){
string n;
int y,m,d;
cin>>n>>y>>m>>d;
Croster c1(n,y,m,d);
c1.show();
}else if(cmd==2){
string n;
int y,m,d;
cin>>n>>y>>m>>d;
Date tdate(y,m,d);
Croster c2(n,tdate);
c2.show();
}else if(cmd==-1){
break;
}
}
}
重要知识点:
构造函数的重载;
E 友元类
答案:
#include<iostream>
using namespace std;
class Subject //选课类
{
private:
double score[3]; //3门课成绩
const int SMath, SEng, SCpp; //3门课的学分,分别为4、2、2
public:
Subject(double score_math = 0, double score_eng = 0, double score_cpp = 0);
void Input(); //输入3门课的成绩
friend class Student; //友元类
};
Subject::Subject(double score_math, double score_eng, double score_cpp):SMath(4),SEng(2),SCpp(2)
{
score[0]=score_math;
score[1]=score_eng;
score[2]=score_cpp;
}
void Subject::Input()
{
cin>>score[0]>>score[1]>>score[2];
}
class Student
{
private:
string ID; //学号
string name; //姓名
double GPA; //平均学分积=(成绩1x学分1+成绩2x学分2+成绩3x学分3)/(学分1+学分2+学分3)
public:
Student(string id = "00000", string na = "Noname");
void CalculateGPA(const Subject &sub); //计算平均学分积
void Input(); //输入学号和姓名
void Show(const Subject &sub)const; //输出所有信息
};
Student::Student(string id,string na)
{
ID=id;
name=na;
}
void Student::CalculateGPA(const Subject &sub)
{
GPA=(sub.score[0]*sub.SMath+sub.score[1]*sub.SEng+sub.score[2]*sub.SCpp)/((sub.SMath+sub.SEng+sub.SCpp)*1.0);
}
void Student::Input()
{
cin>>ID>>name;
}
void Student::Show(const Subject &sub)const
{
cout<<"ID: "<<ID<<", Name: "<<name<<endl;
cout<<"Math Eng Cpp"<<endl;
cout<<sub.score[0]<<" "<<sub.score[1]<<" "<<sub.score[2]<<endl;
cout<<"GPA: "<<GPA<<endl;
}
int main()
{
int n; //学生人数
cin >> n;
Student *stu = new Student[n];
Subject *sub = new Subject[n];
for (int i = 0; i < n; i++)
{
stu[i].Input();
sub[i].Input();
}
for (int i = 0; i < n; i++)
{
stu[i].CalculateGPA(sub[i]);
stu[i].Show(sub[i]);
}
delete[] stu;
delete[] sub;
return 0;
}
重要知识点:
很普通的一道题…
F 旅馆人数统计
答案:
#include<iostream>
using namespace std;
class Hotel{
private:
string name;
int id;
public:
static int total;
void add(string n);
static int getTotal();
string getName();
void print();
};
//静态数据成员必须在类外定义和初始化,以及在类内声明。
int Hotel::total=0;
void Hotel::add(string n)
{
name=n;
total++;
id=total;
}
int Hotel::getTotal()
{
return total;
}
string Hotel::getName()
{
return name;
}
void Hotel::print()
{
cout<<id<<" "<<name<<" "<<total;
}
int main(){
Hotel h[100];
h[0].add("Susan");
h[1].add("Peter");
h[2].add("John");
h[3].add("Mary");
h[4].add("Alice");
string name;
cin>>name;
for(int i=0;i<Hotel::getTotal();i++)
{
if(h[i].getName()==name)
{
h[i].print();
break;
}
}
return 0;
}
重要知识点:
·静态数据成员:
静态数据成员是属于类的成员,而不是属于类的实例或对象的成员。静态数据成员在类的所有实例之间共享,并且可以通过类名来访问,而不是通过类的实例或对象来访问。
静态数据成员通常用于跟踪类的共享数据或状态,比如计数器、常量等。在类声明中,可以使用关键字static来声明静态数据成员。
静态数据成员必须在类外定义和初始化,以及在类内声明。
·静态函数成员:
静态成员函数是属于整个类,可以直接通过类名调用。
无法访问非静态数据成员或非静态成员函数。
静态成员函数的声明和定义方式与非静态成员函数类似,但在声明和定义时需要使用static关键字标识。同时,静态成员函数可以被继承并重载。
·类的数组;
G TestConstClass
答案:
#include<iostream>
#include<string>
using namespace std;
class TestConstClass{
private:
string test;
public:
void set(string t);
string get() const;
};
void TestConstClass::set(string t)
{
test=t;
}
string TestConstClass::get() const
{
return test;
}
void print (const TestConstClass & tcs) {
cout << tcs.get()<<'\n';
}
int main() {
string tmp;
TestConstClass tcs;
while(cin>>tmp){
tcs.set(tmp);
print(tcs);
}
return 0;
}
重要知识点:
常成员函数:
声明格式: 类型 函数名(形参表) const;
(1)是只读函数,不能修改数据成员的值,也不能调用该类中非常成员函数。
(2)const关键字是函数类型的一部分,定义部分中也要带const关键字。
(3)const关键字可以区分重载。
(4)能被常对象和一般对象调用。