C++ 输入输出,字符串和面向对象

2018-06-09  本文已影响0人  CaptainRoy
#include <iostream>

using namespace std;

int main(int argc, const char * argv[]) {
    
    cout << "Hello, World! \n";
    cout << "Hello C++ " << endl; // endl 代替 \n
    
    return 0;
}
/*
提示输入一个整数,该整数分别以八进制,十进制,十六进制打印出来
 */
int x = 0;
cout << "请输入一个整数:" << endl;
cin >> x;
cout << "八进制 : " << oct << x << endl;
cout << "十进制 : " << dec << x << endl;
cout << "十六进制 : " << hex << x << endl;
    
/*
提示输入一个布尔值(0或1) 以布尔方式打印出来
*/
bool y = false;
cout << "输入一个布尔值(0,1): "<<endl;
cin>>y;
cout<<"输入的布尔值为: "<<boolalpha<<y<<endl;
class Coordinate
{
public:
    int x;
    int y;
    void printX()
    {
        cout << x << endl;
    }
    
    void printY()
    {
        cout << y << endl;
    }
};
// 栈实例化对象
Coordinate coor;
coor.x = 10;
coor.y = 20;
coor.printX();
coor.printY();
    
// 堆实例化对象
Coordinate *pCoor = new Coordinate();
pCoor->x = 100;
pCoor->y = 200;
pCoor->printX();
pCoor->printY();
#include <iostream>
#include <string>

using namespace std;

class Student
{
public:
    string name;
    int age;
    
    void introduce()
    {
        cout  << "我的名字是 :"+name+" 今年" << age << "岁"<<endl;
    }
};

int main(int argc, const char * argv[]) {
    
    Student *s = new Student();
    s->name = "Roy";
    s->age = 18;
    s->introduce();
    
    
    return 0;
}
#include <iostream>
#include <string>

using namespace std;

int main(int argc, const char * argv[]) {
    
    string name = "Roy";
    string hello = "Hello";
    string greet = name + hello;
    cout << greet << endl;
    
    return 0;
}
    string name;
    cout << "请输入您的名字: "<<endl;
    cin >> name; // Roy
    cout << "Hello "+name << endl; // Hello Roy
    cout << "您的第一个字母是: "<<name[0]<<endl; // 您的第一个字母是: R
上一篇下一篇

猜你喜欢

热点阅读