c++小例子

Person.h
<pre name="code" class="cpp">

#ifndef __PERSON__
#define __PERSON__

#include <iostream>
#include <cstring>

using namespace std;
class Person{
    public:
    string name;
 virtual void sayHello();
 void test();
 Person(string name){
  name=name;
 }
virtual  ~Person(){
    cout<<"person destructor \n";
 }
};


#endif
 

Girl.h

#ifndef __GIRL__
#define __GIRL__

#include <iostream>
#include <cstring>
#include "Person.h"
using namespace std;

class Girl:public Person{
public :
    string sex;
    virtual void sayHello();
    Girl(string name,string sex):Person(name),sex(sex){}
    virtual ~Girl(){
    cout<<"girl destructor \n";
    }

};

#endif

Person.cpp

<pre name="code" class="cpp">#include <iostream>
#include <cstring>
#include "Person.h"
using namespace std;

 void Person::sayHello(){
  cout<<"---i am a person my name is "<<name;
}

void Person::test(){
cout<<"---i am testing";
}

 Girl.cpp 

#include <iostream>
#include <cstring>
#include "Girl.h"
#include "Person.h"
using namespace std;
void Girl::sayHello()
{
    test();
    cout<<"---hi , i am a girl \n";
}

main.cpp

#include <iostream>
#include <cstring>
#include "Girl.h"
#include "Person.h"
using namespace std;

int main()
{
    Person * p=new Girl("lily","nv");
    p->sayHello();
    delete p;
    return 0;
}

运行结果:

---i am testing---hi , i am a girl
girl destructor
person destructor



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
c++语言实例#include <iostream.h> const int N=1000000; class set { int IntArray[N]; int last; public: int Append(); int Delete(); int Nember(int x); int Empty(); int Full(); void Display(); void find(); set(); }; int set::Nember(int x) { int i; for(i=0;i<last;i++) if(IntArray[i]==x) return 1; return 0; } set::set() { last=0; } int set::Empty() //判断集合是否空 { if(last==0) return 1; return 0; } int set::Full() { if(last==N) return 1; return 0; } int set::Append() { if(Full()) return 0; int x; cout<<"请输入你要添的数"; cin>>x; if(Nember(x)) return 0;// 集合中已经有x IntArray[last]=x; last++; return 1; // 插入x } int set::Delete() { if(Empty()) { cout<<"数组为空"; return 0; } int i; int x; int j; cout<<"请输入你要删除的数"; cin>>x; for(i=0;i<last;i++) if(IntArray[i]==x) { for(j=i;j<last;j++) IntArray[j]=IntArray[j+1]; last--; cout<<"删除后:"<<endl;; Display(); return 1; } cout<<"集合中没有你要删除的数!"<<endl; return 0; } void set::Display() { int i; for(i=0;i<last;i++) { cout<<IntArray[i]<<'\t'; if((i+1)%5==0) cout<<endl; } cout<<endl; } void set::find() { if(Empty()) { cout<<"数组为空"; } else { int x; int i; cout<<"请输入要查找的数:"; cin>>x; for(i=0;i<last;i++) if(IntArray[i]==x) break; if(i>=last) cout<<"没有查到!"<<endl; else cout<<"找到了,在数组第"<<i+1<<"位置"<<endl; } } void main() { set b; b.Append(); b.Append(); b.Append(); b.Append(); b.Append(); b.Append(); b.Append(); b.Append(); b.Display(); b.Display(); b.Display(); b.Display(); b.Display(); b.Display(); b.Delete(); b.Delete(); b.Delete(); b.Delete(); b.Delete(); b.Delete(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值