C++第15章
Posted 自动秃头化
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++第15章相关的知识,希望对你有一定的参考价值。
1.
#include<iostream>
using namespace std;
class A{
public:
int value;
A(int n){
value=n;
A::V=A::V+1;
cout<<value<<endl;
}
void print(){
cout<<A::V<<endl;
}
~A(){
A::V=A::V-1;
cout<<A::V<<endl;
}
private:
static int V;
};
int A::V=0;
int main(){
A a1(5);
A a2(80);
A a3(20);
a1.print();
}
2.
#include<iostream>
using namespace std;
class A{
public:
int value;
A(int n){
value=n;
A::V=A::V+1;
cout<<value<<endl;
}
int Get(void) const{
return value;
}
int GetStatic(void) const{
return A::V;
}
void print(){
cout<<A::V<<endl;
}
~A(){
A::V=A::V-1;
cout<<A::V<<endl;
}
private:
static int V;
};
int A::V=0;
int main(){
A a1(5);
a1.Get();
a1.GetStatic();
A a2(80);
a2.Get();
a2.GetStatic();
A a3(20);
a3.Get();
a3.GetStatic();
a1.print();
}
3.
#include<iostream>
using namespace std;
class Animal{
private:
int weight;
int age;
public:
friend int SetValue(Animal& a,int s){
a.weight=s;
a.age=0;
}
friend int SetValue(Animal& t, int b, int c){
t.weight=b;
t.age=c;
}
void show(){
cout<<"itsWeight="<<weight<<endl;
cout<<"itsAge="<<age<<endl;
}
};
int main(){
Animal peppy;
SetValue(peppy, 5);
peppy.show();
SetValue(peppy,7,9);
peppy.show();
}
4.
#include<iostream>
using namespace std;
class Animal{
public:
void SetValue(int v){
itsWeight=v;
itsAge=0;
}
void SetValue(int v, int w){
itsWeight=v;
itsAge=w;
}
void show(){
cout<<"itsWeight="<<itsWeight<<endl;
cout<<"itsAge="<<itsAge<<endl;
}
protected:
int itsWeight;
int itsAge;
};
int main(){
Animal peppy;
peppy.SetValue(5);
peppy.show();
peppy.SetValue(7,9);
peppy.show();
}
5.
#include<iostream>
using namespace std;
class Car{
public:
int m;
Car(int j){
size=j;
m=j;
}
protected:
int size;
};
class Boat{
public:
int m;
Boat(int j){
size=j;
m=j;}
protected:
int size;
};
int main(){
Car c1(2);
Boat b1(3);
int time=4;
cout <<time*c1.m*b1.m;
}
6.
#include<iostream>
#include<cstring>
using namespace std;
class Student{
private:
static int sum_score;
static int count;
public:
char name[10];
int score;
int m;
int t;
Student(char* n,int c)
{
strcpy(name,n);
score=c;
}
void sum(){
sum_score+=score;
count++;
}
static double aver(){
cout<<Student::sum_score<<"," <<count<<endl;
return (double) sum_score/count;
}
};
int Student::sum_score=0;
int Student::count=0;
int main()
{
Student stu[5] ={Student("Ma",89),Student("Hu",90),Student("LU",95),
Student("Li",88),Student("Gao",75)};
for(int i=0;i<5;i++)
stu[i].sum();
cout<<"Average="<<Student::aver()<<endl;
}
7.
#include<iostream>
#include<cstring>
using namespace std;
class Student{
private:
static int sum_score;
static int count;
public:
char name[10];
int score;
int m;
int t;
Student(char* n,int c)
{
strcpy(name,n);
score=c;
}
void sum(){
sum_score+=score;
count++;
}
static double aver(){
cout<<Student::sum_score<<"," <<count<<endl;
return (double) sum_score/count;
}
};
int Student::sum_score=0;
int Student::count=0;
int main()
{
Student stu[5] ={Student("Ma",89),Student("Hu",90),Student("LU",95),
Student("Li",88),Student("Gao",75)};
for(int i=0;i<5;i++)
stu[i].sum();
cout<<"Average="<<Student::aver()<<endl;
}
8.
#include<iostream>
using namespace std;
class MyTime{
friend class MyDate;
public:
int hour;
int minute;
int second;
MyTime(int s,int f,int m)
{
hour=s;
minute=f;
second=m;
}
};
class MyDate{
public:
int year=2021;
int month;
int day;
MyDate(int m,int d){
month=m;
day=d;
}
void Display(MyTime &r){
cout<<year<<"/"<<month<<"/"<<day<<" ";
cout<<r.hour<<":"<<r.minute<<":"<<r.second<<endl;
}
};
int main()
{
MyTime t(10,30,45);
MyDate d(4,20);
d.Display(t);
}
以上是关于C++第15章的主要内容,如果未能解决你的问题,请参考以下文章