This is just a simple object instance exercise, previously written with an object and other knowledge points using the example http://blog.csdn.net/qingbowen/article/details/46126549 (Easy learner management system).
Main.cpp
#include <iostream> #include "car.h" int main (int argc, char** argv) {//Instantiate an object car car01;//assigns a value to the object Car01.name= "Buick"; Car01.age=1;car01.color= "Blue";//Output std::cout<< "Car01 Yes:" <<car01.desc () <<std::endl; Define a pointer object car *car02;//get Car01 's pointer with "&" car02=&car01; Here's "." To Change to "car02->age=2;std::cout<<" Car01 is: "<<car01.desc () <<std::endl; std::cout<< "Car02 is:" <<car02->desc () <<std::endl; return 0;}
Car.h
#ifndef car_h#define car_h#include <string>class car{public:std::string name;int age;std::string color;std:: String desc ();p rotected:}; #endif
Car.cpp
#include "car.h" #include <sstream>std::string car::d ESC () {Std::stringstream Agestr;agestr<<age;return " Name--> "+name+",age--> "+agestr.str () +",color--> "+color; }
Debug:
18th, exercises for object and object pointers (c + +)