8.5
#include<iostream>
#define PI 3.14
using namespace std;
class Yuan{
private:
double r;
double h;
double V;
public:
void setr()
{
cin>>r;
}
void seth()
{
cin>>h;
}
void cal()
{
V=r*r*PI*h;
}
void display()
{
cout<<"h:"<<h<<" "<<"r:"<<r<<" "<<"V:"<<V<<endl;
}
};
int main()
{
Yuan y1;
cout<<"r set"<<endl;
y1.setr();
cout<<"h set"<<endl;
y1.seth();
y1.cal();
y1.display();
return 0;
}
![](https://img-blog.csdnimg.cn/direct/d712a47f0d0d40ef9b3057e08a51acd9.jpeg)
8.1
#include<iostream>
using namespace std;
class Count{
private:
string countname;
string code;
public:
void set(string count_name,string _code)
{
countname=count_name;
code=_code;
}
void display()
{
cout<<"CountName:"<<countname<<endl;
cout<<"Code:"<<code;
}
};
int main()
{
Count c1;
cout<<"name:";
string name1;
cin>>name1;
cout<<"code:";
string code1;
cin>>code1;
c1.set(name1,code1);
c1.display();
return 0;
}
![](https://img-blog.csdnimg.cn/direct/51cf5cfb83b94588a1f2bc7c92984716.jpeg)
8.2
#include<iostream>
using namespace std;
class CStudent{
public:
string name;
int age;
};
int main()
{
CStudent stu;
CStudent &nst=stu;
nst.name="xiaoli";
nst.age=13;
cout<<"对象输出name:"<<stu.name<<endl;
cout<<"对象输出age:"<<stu.age<<endl;
cout<<"引用输出name:"<<nst.name<<endl;
cout<<"引用输出age:"<<nst.age<<endl;
CStudent *pst=new CStudent();
pst->name="zhangsan";
pst->age=14;
cout<<"对象输出name:"<<pst->name<<endl;
cout<<"对象输出age:"<<pst->age<<endl;
cout<<"指针输出name:"<<(*pst).name<<endl;
cout<<"指针输出age:"<<(*pst).age<<endl;
return 0;
}
![](https://img-blog.csdnimg.cn/direct/1dcccfa528a245ba8d00dfef8b248f2f.jpeg)
8.3
#include<iostream>
using namespace std;
class Csstudent{
private:
string name;
int age;
string grade;
public:
Csstudent(string _name,int _age,string _grade)
{
name=_name;
age=_age;
grade=_grade;
}
void display()
{
cout<<"name:"<<name<<" "<<"age:"<<age<<" "<<"grade:"<<grade<<endl;
}
};
int main()
{
Csstudent stu1("lihao",14,"G1"),stu2("nige",15,"G2");
stu1.display();
stu2.display();
return 0;
}
![](https://img-blog.csdnimg.cn/direct/0fdbc53375ff4207b6907d9ca5fd199e.jpeg)
8.4
#include<iostream>
using namespace std;
class Juxing{
private:
double length;
double weith;
double area=0;
double leng=0;
public:
Juxing(double ch,double ku):length(ch),weith(ku){}
void calleng()
{
leng=(length+weith)*2;
}
void caarea()
{
area=length*weith;
}
void display()
{
cout<<"c:"<<length<<" "<<"k:"<<weith<<" "<<"len:"<<leng<<" "<<"area:"<<area<<endl;
}
};
int main()
{
double a1,b1;
cin>>a1>>b1;
Juxing j1(a1,b1);
j1.caarea();
j1.calleng();
cin>>a1>>b1;
Juxing j2(a1,b1);
j2.calleng();
j2.caarea();
j1.display();
j2.display();
return 0;
}
8.5
#include<iostream>
using namespace std;
class libera{
private:
char m_name[64];
public:
libera(const char* name)
{
strcpy(m_name,name);
}
libera(const libera& book)
{
strcpy(m_name,book.m_name);
}
void set()
{
cin>>m_name;
}
void display()
{
cout<<m_name<<endl;
}
};
int main()
{
libera l1("alice runing");
libera l2(l1);
l1.display();
l2.display();
cout<<"进行修改"<<endl;
l1.set();
l2.display();
return 0;
}
![](https://img-blog.csdnimg.cn/direct/177238dcbf9240e486f245ab1c1b57ee.jpeg)