18th, exercises for object and object pointers (c + +)

Source: Internet
Author: User

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).


#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;}

#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

#include "car.h" #include <sstream>std::string car::d ESC () {Std::stringstream Agestr;agestr<<age;return " Name--> "+name+",age--> "+agestr.str () +",color--> "+color; }

18th, exercises for object and object pointers (c + +)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.