【实验目的】
(1)掌握指针的概念,会定义和使用指针变量
(2)指针与对象结合的使用
1、程序设计题:编写函数fun(char *s),功能是把s所指字符串中的内容逆置。例如:字符串中原有的字符串为:abcdefg,则调用该函数后, 串中的内容为:gfedcba
#include <string.h>
#include <stdio.h>
#define N 81
void fun ( char *s ) {
int i, j;//写入你的代码
char t;
for (i = 0, j = strlen(s) - 1; i < j; i++, j--)
{
t = s[i];
s[i] = s[j];
s[j] = t;
}
}
void main(void) {
char a[N];
printf ( "Enter a string : " ); gets ( a );
printf ( "The original string is : " ); puts( a );
fun ( a );
printf("\n");
printf ( "The string after modified : ");
puts ( a );
}

字符串逆置:
void fun ( char *s ) {
int i, j;//写入你的代码
char t;
for (i = 0, j = strlen(s) - 1; i < j; i++, j--)
{
t = s[i];
s[i] = s[j];
s[j] = t;
}
}
2、用公有继承的方式,程序运行的时候 输入num,name,sex,age,addr的值,程序输出5个数据的值
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
void get_value() {
cout << "Please enter num, name, sex: ";
cin >> num >> name >> sex;
//添加代码 实现 num,name和sex的输入
}
void display( ) {
cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;
}
private :
int num;
string name;
string sex;
};
class Student1 : public Student //增加公有继承
{
public:
void get_value_1() {
get_value();
cout << "Please enter age, address: ";
cin >> age >> addr;
//添加代码。实现基类继承的成员的输入以及本类新增的数据成员的输入
}
void display_1() {
display();
cout << "age: " << age << endl;
cout << "address: " << addr << endl;
//添加代码。实现基类继承成员和新增age,addr的输出
}
private:
int age;
string addr;
};
void main() {
Student1 stud1;
//stud1.get_value();
stud1.get_value_1();
//stud1.display();
stud1.display_1();
}

子类的对象可以调用基类的成员函数,所以主函数中声明的是子类的对象。
注意:
公有继承得到的子类成员函数中包含使用基类成员函数的书写方法。
void get_value_1() {
get_value();
cout << "Please enter age, address: ";
cin >> age >> addr;
//添加代码。实现基类继承的成员的输入以及本类新增的数据成员的输入
}
3、赋值兼容性原则(public继承)
① 派生类的对象可以赋值给基类对象。(派生类对象是基类对象)
② 派生类的对象可以初始化基类的引用。
③ 指向基类的指针也可以指向派生类。
- 派生类对象的地址可以赋值给基类指针
base * pb = &d;
▲通过基类对象名、指针和引用只能使用从基类继承的成员
#include <iostream>
using namespace std;
class B0 { //基类B0声明
public:
void display() { cout << "B0::display()" << endl; } //公有成员函数
};
class B1 : public B0 {
public:
void display() { cout << "B1::display()" << endl; }
};
void fun(B0* ptr) {
ptr->display(); //"对象指针->成员名"
}
void main()
{ //主函数
B0 b0; //声明B0类对象
B1 b1; //声明B1类对象
B0* p; //声明B0类指针
p = &b0; //B0类指针指向B0类对象①
fun(p);
p = &b1; //B0类指针指向B1派生类对象②
fun(p);
b0 = b1;//派生类对象赋值给基类对象③
b0.display();
B0& x = b1; //派生类对象初始化基类对象的引用④
x.display();
b1.display();//派生类对象⑤
}

分析:
5次调用display()函数,通过不同的对象不同的方式;
①是基类对象的指针指向基类对象;②③④是三条赋值兼容性规则;
①②③④调用基类的display()函数;通过基类对象名、指针和引用只能使用从基类继承的成员
⑤是派生类的对象,通过对象名调用派生类的成员函数display();
当派生类与基类有形同的成员时,若未强行指名,则通过派生类的对象使用的是派生类中的同名成员。
如果要通过派生类的对象访问基类中被覆盖的同名成员,应使用类名限定。

5、编写程序:定义Person类,数据成员包含保存姓名的变量name,并有能够输出姓名的成员函数PrintName()。
从Person类派生出Worker类,包括数据成员number记录工号、sex记录性别、age记录年龄、add记录家庭住址;函数成员printinfor()输出全部个人信息。
要求:在Worker类的printinfor()成员函数中须调用Person类的成员函数PrintName()。主函数中,构造Worker类对象,并输出该对象的工号、年龄、家庭住址等信息。
#include <string>
#include <iostream>
using namespace std;
class Person
{
private:
char* name;
public:
Person(char* n1) :name(n1) {}
void PrintName() { cout << "姓名:" << name; }
};
class Worker :public Person
{
private:
long int Number; int age; char* sex;char* add;
public:
Worker(char* n2, long int n3, int n4, char* n5, char* n6) :Person(n2) { Number = n3; age = n4; sex = n5; add = n6; }
void PrintInfor() { cout << "学号:"<<Number<<endl<<"年龄:"<<age<<endl<<"性别:"<<sex<<endl<<"地址号:"<<add<< endl; /*this->PrintName();*/ }
};
int main()
{
char ch[] = "张三";
Person a(ch);
a.PrintName();
cout << endl;
char m[]= "女";
char n[]= "平安街道310号";
Worker b(ch, 142180217,19,m,n);
b.PrintInfor();
cout << endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
class Person {
public:
//Person() {}
Person(string n):name(n) {}
void PrintName();
private:
string name;
};
void Person::PrintName()
{
cout << "Name of person:" << name<<endl;
}
class Worker :public Person
{
public:
Worker(string n1,int num, int a, string ad, string s) :Person(n1),number(num), age(a), add(ad), sex(s) {}
void Printinfor()
{
PrintName();
cout << "number of student:" <<number<<endl;
cout << "age of student:" <<age<<endl;
cout << "add of student:" <<add<<endl;
cout << "sex of student:" << sex << endl;
}
private:
int number, age;
string add;
string sex;
};
int main()
{
Worker c("lili",03,18,"永安街", "女");
c.Printinfor();
return 0;
}
注意:
派生类从基类继承数据成员、函数成员:
①
Worker(string n1,int num, int a, string ad, string s) :Person(n1),number(num), age(a), add(ad), sex(s) {}
派生类的构造函数中包含了需要继承的基类成员name,且命名形参变量(n1)不能与基类构造函数中声明的name的形参(n)一样。
赋值:Person(n1) 基类名(被继承数据成员形参名)
在派生类构造函数的初始化列表里调用基类的构造函数
②主函数声明对象:
Worker c("lili",03,18,"永安街", "女");
派生类名 对象名(数据成员值,与派生类构造函数声明的数据成员个数、名称相对应)