C++快速入门 课件源码讨论之静态属性和静态方法
#include <iostream>#include <string>
class Pet
{
public:
Pet(std::string theName);
~Pet();
static int getCount();
protected:
std::string name;
private:
static int count;
};
class Dog : public Pet
{
public:
Dog (std::string theName);
};
class Cat: public Pet
{
public:
Cat(std::string theName);
};
int Pet::count = 0;
Pet::Pet(std::string theName)
{
name = theName;
count++;
std::cout << "一只宠物出生了,名字叫做:" << name << "\n";
}
Pet::~Pet()
{
count--;
std::cout << name << "挂掉了\n";
}
int Pet::getCount()
{
return count;
}
Dog::Dog(std::string theName) :Pet(theName)
{
std::cout << this<< std::endl;
}
Cat::Cat(std::string theName) : Pet(theName)
{
}
int main()
{
Dog dog("Tom");
Cat cat("Jerry");
std::cout << &dog;
std::cout << "\n已经诞生了" << Pet::getCount() << "只宠物!\n";
Dog dog2("TT");
Cat cat2("JJ");
std::cout << "\n现在呢 ,已经诞生了" << Pet::getCount() << "只宠物!\n";
std::cout << "\n现在还剩下" << Pet::getCount() << "只宠物!\n";
return 0;
}
#include <iostream>
#include <string>
class BaseClass
{
public:
BaseClass();
~BaseClass();
void doSomething();
};
class SubClass :public BaseClass
{
public:
SubClass();
~SubClass();
};
BaseClass::BaseClass()
{
std::cout << "进入基类构造器\n";
std::cout << "基类构造器\n";
}
BaseClass::~BaseClass()
{
std::cout << "进入基类析构器\n";
std::cout << "基类析构器\n";
}
void BaseClass::doSomething()
{
std::cout << "我在吃东西\n";
}
SubClass::SubClass()
{
std::cout << "进入子类构造器\n";
std::cout << "子类构造器\n";
}
SubClass::~SubClass()
{
std::cout << "进入子类析构器\n";
std::cout << "子类析构器\n";
}
int main()
{
SubClass subclass;
subclass.doSomething();
std::cout << "完事!\n";
system("pause");
return 0;
}继承机制中的构造器和析构器
简单说一下我的理解吧
本来觉得已经理解了,可是仅仅换了一个名字就白痴了。。。。
在类继承里面是这样的#include <iostream>
#include <string>
class animal
{
public:
std::string mouth;
void eat();
void sleep();
void drool();
};
class pig:public animal
{
public:
void climb();
void eat();
};
class turtle :public animal
{
public:
void swim();
};
void animal::eat()
{
std::cout << "I'm eattimg!" << std::endl;
}
void animal::sleep()
{
std::cout << "I'm sleeping!" <<std:: endl;
}
void animal::drool()
{
std::cout << "I'm drooling" << std::endl;
}
void pig::eat()
{
std::cout << "吃了你!" << std::endl;
}
void pig :: climb()
{
std::cout << "I'm climbing" << std::endl;
}
void turtle::swim()
{
std::cout<<"I'm swimming"<< std::endl;
}
int main()
{
pig a;
turtle b;
a.eat();//在调用的时候会调用void pig::eat()方法,而void animal::eat()会被前者覆盖
a.mouth = "chang";
b.mouth = "bian";
a.climb();
a.drool();
a.sleep();
b.swim();
b.drool();
b.sleep();
std::cout<<a.mouth<<std::endl;
std::cout<<b.mouth<<std::endl;
system("pause");
}
#include <iostream>
#include <string>
class Pet
{
public:
Pet(std::string theName);
void eat();
void sleep();
virtualvoid play();<font style=""><font size="5"><font style="background-color: black;"><font color="cyan">//<strong>需要注意的是虚方法的使用</strong></font></font></font></font>
protected:
std::string name;
private:
};
class Cat :public Pet
{
public:
Cat(std::string theName);
void climb();
void play();
private:
};
class Dog :public Pet
{
public:
Dog(std::string theName);
void bark();
void play();
private:
};
Pet::Pet(std::string theName)
{
name = theName;
}
void Pet::eat()
{
std::cout << name << "正在吃东西!\n";
}
void Pet::sleep()
{
std::cout << name << "正在睡大觉!\n";
}
void Pet::play()
{
std::cout << name << "正在玩游戏!\n";
}
Cat::Cat(std::string theName) :Pet(theName)
{
}
void Cat::climb()
{
std::cout << name << "正在爬树!\n";
}
void Cat::play()
{
std::cout << name << "正在玩毛毛球!\n";
}
Dog::Dog(std::string theName) :Pet(theName)
{
}
void Dog::bark()
{
std::cout << name << "汪汪汪汪!\n";
}
void Dog::play()
{
std::cout << name << "正在追赶那只该死的猫!\n";
}
int main()
{
Pet *cat = new Cat("加菲猫");
Pet *dog = new Dog("欧迪");
cat->sleep();
cat->eat();<strong><font color="cyan" style="background-color: black;">//这里如果是用Cat cc;cc.eat();结果是什么呢?自己改一下就知道了<img alt="" src="http://bbs.fishc.com/static/image/smiley/lovely/20080925104601644.gif" border="0" smilieid="105"></font></strong>
cat->play();
dog->sleep();
dog->eat();
Dog dd("bb");
dd.bark();
dog->play();
delete cat;
delete dog;
return 0;
}
这段代码是我自己在网上搜索的
给大家做个参考
#include <iostream>
using namespace std;
class Complex
{
public:
Complex() { real = 0; imag = 0; }
Complex(double r, double i) { real = r; imag = i; }
operator double() { return real; } //类型转换函数
private :
double real;
double imag;
};
int main()
{
Complex c1(3, 4), c2(5, -10), c3;
double d;
d = 2.5 + c1;//要求将一个double数据与Complex类数据相加
cout << d << endl;
return 0;
}
shaoguanfa 发表于 2015-1-21 19:23
这段代码是我自己在网上搜索的
给大家做个参考
是不是跟你想象的有点不一样?我也很疑惑呢,
operator double() { return real; } //类型转换函数
d = 2.5 + c1;//要求将一个double数据与Complex类数据相加
还没理解。。。。{:5_92:} #include <iostream>
using namespace std;
void main()
{
cout<<"第一章"<<endl;
cout<<" ";
cout.setf(ios::left);//设置对齐方式为left
cout.width(7);//设置宽度为7,不足用空格填充
cout<<"1.1";
cout<<"什么是C语言";
cout.unsetf(ios::left); //取消对齐方式,用缺省right方式
cout.fill('.'); //设置填充方式
cout.width(30); //设置宽度,只对下条输出有用
cout<<1<<endl;
cout<<" ";
cout.width(7); //设置宽度
cout.setf(ios::left); //设置对齐方式为left
cout.fill(' '); //设置填充,缺省为空格
cout<<"1.11";
cout<<"C语言的历史";
cout.unsetf(ios::left); //取消对齐方式
cout.fill('.');
cout.width(30);
cout<<58<<endl;
cout.fill(' ');
cout<<"第二章"<<endl;
}
这是一段控制格式输出的代码感觉还不错输出结果如下
这是文件写入并读取的代码,希望对初学者有点帮助
<p> <div class="blockcode"><blockquote>#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string s;
cout << "请输入要写入的内容:" << endl;
getline(cin, s);
fstream p("a.txt", ios::in | ios::out|ios::app );
p << s << endl;
cout << "读取的内容是:" << endl;
static char line;
p.seekg(ios::beg);
while (!p.eof())
{
p.getline(line,222);
cout << line<<endl ;
}
p.close();
system("pause");
return 0;
}
多继承与虚继承{:5_92:}
#include <iostream>
#include <string>
class Person
{
public:
Person(std::string theName);
void introduce();
protected:
std::string name;
};
class Teacher :virtual public Person
{
public:
Teacher(std::string theName, std::string theClass);
void teach();
void introduce();
protected:
std::string classes;
};
class Student :virtual public Person
{
public:
Student(std::string theName, std::string theClass);
void attendClass();
void introduce();
protected:
std::string classes;
};
class TeachingStudent :public Student, public Teacher
{
public:
TeachingStudent(std::string theName, std::string classTeaching, std::string classAttending);
void introduce();
};
Person::Person(std::string theName)
{
name = theName;
}
void Person::introduce()
{
std::cout << "大家好,我是" << name << "。\n";
}
Teacher::Teacher(std::string theName, std::string theClass) :Person(theName)
{
classes = theClass;
}
void Teacher::teach()
{
std::cout << name << "教" << classes << "。\n";
}
void Teacher::introduce()
{
std::cout << "大家好,我是" << name << ",我教" << classes << "。\n";
}
Student::Student(std::string theName, std::string theClass) :Person(theName)
{
classes = theClass;
}
void Student::attendClass()
{
std::cout << name << "加入" << classes << "学习。\n";
}
void Student::introduce()
{
std::cout << "大家好,我是" << name << ",我在" << classes << "学习。\n";
}
TeachingStudent::TeachingStudent(std::string theName,
std::string classTeaching, std::string classAttending) :Teacher(theName, classTeaching), Student(theName, classAttending),Person(theName)
{
}
void TeachingStudent::introduce()
{
std::cout << "大家好,我是" << name << "。我教" << Teacher::classes << ","<<"同时,我在"<<Student::classes<<"学习。\n";
}
int main()
{
Teacher teacher("天天", "初级语言");
Student student("小白", "初级语言");
TeachingStudent teachingstudent("飞鱼", "初级语言", "高级语言");
teacher.introduce();
teacher.teach();
student.introduce();
student.attendClass();
teachingstudent.introduce();
teachingstudent.teach();
teachingstudent.attendClass();
return 0;
} 学习下c++,
页:
[1]