#include <iostream>
#include <string>
#include "dynamic_memory.h"
Company::Company(std::string theName)
{
name = theName;
};
void Company::printInfo()
{
std::cout << "this company's name is " << name << std::endl;
};
TechCompany::TechCompany(std::string theName, std::string theproduct) : Company(theName)
{
product = theproduct;
};
void TechCompany::printInfo()
{
std::cout << "this company's name is " << name << " his product is " << product << std::endl;
}
int main()
{
Company *company = new Company("Apple");
company->printInfo();
delete company;
company = NULL;
company = new TechCompany("Apple", "Iphone");
company->printInfo();
delete company;
company = NULL;
}
/* vim: set ts=4 sw=4 sts=4 tw=100 */
预处理命令,避免重复编译
#ifndef DYNAMIC_MEMORY_H_INCLUDED 名字要保持一致,为了方便团队编程
#define DYNAMIC_MEMORY_H_INCLUDED
class Company
{
public:
Company(std::string theName);
virtual void printInfo();
protected:
std::string name;
};
class TechCompany : public Company
{
public:
TechCompany(std::string theName, std::string product);
virtual void printInfo();
private:
std::string product;
};
#endif // DYNAMIC_MEMORY_H_INCLUDED
命名空间
#include <iostream>
using namespace std;
namespace a
{
void print();
void print(){
cout << "hello world from namespace a" << endl;
}
}
namespace b{
void print();
void print(){
cout << "hello world from namespace b" << endl;
}
}
int main()
{
a::print();
b::print();
std:cout << "hello" << std:endl; 提倡
using namespace std; 整个命名空间全局化违背了命名空间的设计意图。不提倡
using std::cout 只给cout全局性, OK
只放在某个函数里,那么它只能在这一个函数里使用。
}