c++的面向对象基础

来源:互联网 发布:张千帆 宪法 知乎 编辑:程序博客网 时间:2024/04/27 18:56

所有程序均在VC++6.0英文版中运行成功。

1、p329

#include <stdio.h>
#include <iostream.h>
/* 本程序的作用是输出一行字符 */
void main()
{
  printf("This is a c++ program./n");
  cout<<" This is a c++ program./n";//本行输出一行字符
}

2、p331

#include <iostream.h>
void main()
{cout<<"please enter your name and age:"<<endl;
 char name[10];
 int age;
 cin>>name;
 cin>>age;
 cout<<"your name is"<<name<<endl;
 cout<<"your age is "<<age<<endl;
}

3、p333

#include <iostream.h>
int max(int a,int b,int c)         //求3个整数中的最大者
{ if (b>a) a=b;
  if (c>a) a=c;
  return a; }
float max(float a,float b,float c) //求3个实数中的最大者
{ if (b>a) a=b;
  if (c>a) a=c;
  return a; }
long max(long a,long b,long c)     //求3个长整数中的最大者
{ if (b>a) a=b;
  if (c>a) a=c;
  return a; }

void main()
{int a,b,c;float d,e,f;long g,h,i;
 cin>>a>>b>>c;cin>>d>>e>>f;cin>>g>>h>>i;
 int m;
 m=max(a,b,c);                    //函数值为整型
 cout<<"max_i="<<m<<endl;
 float n;
 n=max(d,e,f);
 cout<<"max_f="<<n<<endl;
 long int p;
 p=max(g,h,i);
 cout<<"max_l="<<p<<endl;
}

4、p335

#include <iostream.h>
int max(int a,int b,int c) //求3个整数中的最大者
{if (b>a) a=b;
 if (c>a) a=c;
 return a; }
int max(int a,int b) //求二个整数中的最大者
{if (a>b) return a;
else return b;}

void main()
{int a=7,b=-4,c=9;
 cout<<max(a,b,c)<<endl;  //输出3个整数中的最大者
 cout<<max(a,b)<<endl;    //输出二个整数中的最大者
}

5、p335

#include <iostream.h>
#include <iomanip.h>
void main()
{int a=10;
 int &b=a;               //声明b是a的引用
  a=a*a;
  cout<<a<<setw(6)<<b;  //a的值变化了,b的值也应一起变化
  b=b/5;
  cout<<b<<setw(60)<<a; //b的值变化了,a的值也应一起变化
}

6、p336

void swap(int a,int b)
{int temp;
 temp=a;
 a=b;
 b=temp;                //实现a和b的值互换
}

void main()
{int i=3,j=5;
 swap(i,j);
 cout<<i<<","<<j<<endl; //i和j的值未互换
}

7、p337

#include <iostream.h>
void swap(int &a,int &b)
{int temp;
 temp=a;
 a=b;
 b=temp;
}

void main()
{int i=3,j=5;
swap(i,j);
cout<<"i="<<i<<""<<"j="<<j<<endl;
}

8、p338

#include <iostream.h>
inline int max(int a,int b,int c)     //这是一个内置函数
{ if(b>a) a=b;
  if(c>a) a=c;
  return a;
}

void main()
{int i=7,j=10,k=25,m;
 m=max(i,j,k);
 cout<<"max="<<m<<endl;
}

9、p340

#include <string.h>
struct student
{char name[10];
 int num;
 char sex;
};
void main()
{
student *p;
p=new student;
strcpy(p->name,"Wang Fun");
p->num=10123;
p->sex='M';
delete p;
}

10、p346

#include <string.h>
#include <iostream.h>
void main()
{class stud               //声明一个类
{private:                 //私有部分
   int num;
   char name[10];
   char sex;
  public:                //公用部分
 stud()               //定义构造函数,函数名与类名相同
 {num=10010;          //给数据赋初值
  strcpy(name,"Wang_li");
  sex='F';}

  void display()
  {cout<<"num:"<<num<<endl;
   cout<<"name:"<<name<<endl;
   cout<<"sex:"<<sex<<endl;}
};
stud stud1;              //在定义对象stud1时自动执行构造函数
stud1.display();          //从对象外面调用display函数
}

11、p348

#include <string.h>
#include <iostream.h>
class stud                   //声明一个类
{private:                    //私有部分
 int num;
 char name[10];
 char sex;
public:                       //公用部分
 stud(int n,char nam[],char s)//构造函数
 {num=n;
  strcpy(name,nam);
  sex=s;}
 ~stud()                      //析造函数
 {}

 void display()               //成员函数,输出对象的数据
 {cout<<"num:"<<num<<endl;
  cout<<"name:"<<name<<endl;
  cout<<"sex:"<<sex<<endl;}
};
void main()
{
stud stud1(10010,"Wang_li",'f'),stud2(10011,"Zhang_fun",'m');//建立两个对象
stud1.display();              //输出学生1的数据
stud2.display();              //输出学生2的数据
}

 

12、p349

#include <string.h>
#include <iostream.h>
class stud                         //声明一个类
{private:
 int num;
 char name[10];
 char sex;
public:
  stud(int n,char nam[],char s); //对构造函数的原型声明
  ~stud();                       //对析构函数的原型声明     
  void display();                //对成员函数display的原型声明
};

stud::stud(int n,char nam[],char s) //对构造函数的定义
{num=n;
strcpy(name,nam);
sex=s;}
stud::~stud()                      //对析构函数的定义
{}
void stud::display()               //对成员函数display的定义
{cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
void main()
{
 stud stud1(10010,"Wang_li",'f'),stud2(10011,"Zhang_fun",'m');
 stud1.display();              //输出学生1的数据
    stud2.display();              //输出学生2的数据
}

 

13、p355

#include <iostream.h>
class stud                        //声明基类
{protected:                       //基类保护成员
int num;
char name[10];
char sex;
public:                             //基类公用成员
 void display()                  
 {cout<<"num:"<<num<<endl;
  cout<<"name:"<<name<<endl;
  cout<<"sex:"<<sex<<endl;}
};

class student:public stud       //声明一个公用派生类
{
private:
 int age;
 char addr[30];
public:
 void show()
 {cout<<"num:"<<num<<endl; //引用基类的保护成员,合法。
     cout<<"name:"<<name<<endl; //引用基类的保护成员,合法。
     cout<<"sex:"<<sex<<endl; //引用基类的保护成员,合法。
     cout<<"age:"<<age<<endl; //引用派生类的私有成员,合法。
     cout<<"address:"<<addr<<endl;} //引用派生类的私有成员,合法。
 };
 void main()
 {student a;                       //a是派生类student类中的对象。
  a.show();                        //合法。show是派生类中的公用成员函数。
 
 }

 

 

原创粉丝点击