C++基础语法练习
2014-11-03 19:01
435 查看
#include <iostream> #include <cstdio> using namespace std; class A { public: A(){ cout << "A's default constructor called" << endl; } A(int i) { a = i; cout << "A's constructor called" << endl; } void print() { cout << a; } ~A() { cout << "A's destructor called" << endl; } int getData(){ return a; } private: int a; }; class B:public A { public: B(){ cout << "B's default constuctor called" << endl; } B(int i, int j, int k): A(i), aa(j) { b = k; cout << "B's constructor called" << endl; } void print(); ~B() { cout << "B's destructor called" << endl; } private: int b; A aa; }; void B::print(){ A::print(); cout << b << aa.getData() << endl; } int main() { B bb[2] = { B(1, 2, 3), B(20, 30, 40) }; for(int i = 0; i < 2; i++) bb[i].print(); return 0; } int &f1(int &a) { a += a; return a; } int f2(int b) { b += b; return b; } int main() { int x, y, z; x = 10; y = f1(x); z = f2(x); cout << x << endl; cout << y << endl; cout << z << endl; } class A { private: int a; static int b; public: A(){} A(int i) { a = i; b += i; } void out() { cout << a << b << endl; } }; int A::b = 0; int main() { A obj1(10); obj1.out(); A obj2(15); obj2.out(); obj1.out(); } int i = 0, sum = 0; int main() { for(;;){ i += 2; if(i > 10) break; else if(i == 8) continue; sum += i; } cout << i << sum << endl; } int main() { int a = 1, b = 2, c = 3; int d = 4, y = 10; switch(y) { case 1: a++; break; default: d = 1; case 2: b++; break; case 3: c++; break; } cout << a << b << c << d << endl; } int main() { int i, j, m, n; i = 2; j = 4; m = ++i + j++; n = (++i) + (++j); cout << i << j << m << n << endl; return 0; }
相关文章推荐
- C++基础语法
- c++基础语法--嵌套类
- C++ 学习笔记 <1> C++ 语法基础知识
- C++ FAQS 翻译3 (第一部分 预备知识 第二章 C++语法和语义基础)
- c++基础编程练习11
- [C#]练习了下数组和类,C++ 基础还算可以,不是很吃力
- C++ 类的 this 指针 语法练习2
- C++基础编程练习14
- C++基础语法总结
- c++基础编程练习18
- c++基础语法(构造函数和析够函数)
- C++基础语法知识点归纳
- C语言学习100例实例程序--我练习的C++版(每天一题夯实基础)
- C++ Simplify01-基础语法
- C++基础语法知识点归纳V
- Notes-4:C++练习之LL(1)语法分析
- c++基础学习第三天(c++程序的结构课后练习)
- C++基础语法知识点归纳III
- c++基础编程练习13
- C++ 类的 this 指针 语法练习