构造、析构、继承对对象的影响
2014-04-26 22:08
141 查看
数组
#include <iostream>
#include <string>
#include <vector>
#include <bitset>
using namespace std;
class A{
public:
A(){cout<<"A default constructor"<<endl;}
};
int main()
{
cout<<"------test class-------"<<endl;
A *a = new A[5]; // will call default constructor to initialization, if no default constructor will raise compile error.
cout<<"------test inner type-----"<<endl;
int *b = new int [3](); //initialize
for(int i=0; i<3; i++)
{
cout<<b[i]<<endl;
}
cout<<"------test const array------"<<endl;
const string *c = new const string[2];
cout<<"str1:"<<c[0]<<endl;
cout<<"str2:"<<c[1]<<endl;
return 0;
}
Output:
------test class-------
A default constructor
A default constructor
A default constructor
A default constructor
A default constructor
------test inner type-----
0
0
0
------test const array------
str1:
str2:
STL
#include <iostream>
#include <string>
#include <vector>
#include <bitset>
using namespace std;
int main()
{
cout<<"--------test 1-----------"<<endl;
vector<int> v(3);
cout<<v.size()<<endl;
cout<<sizeof(v)<<endl;
for(vector<int>::iterator iter = v.begin(); iter != v.end(); ++iter)
{
cout<<*iter<<endl;
}
cout<<"--------test 2-----------"<<endl;
vector<int> v2(2,10);
v2.push_back(11);
cout<<v2.size()<<endl;
cout<<sizeof(v2)<<endl;
for(vector<int>::const_iterator iter = v2.begin(); iter != v2.end(); ++iter)
{
cout<<*iter<<endl;
}
cout<<"--------test bitset------"<<endl;
bitset<12> a;
cout<<sizeof(a)<<endl;
cout<<a.size()<<endl;
cout<<a<<endl;
a.set(0);
cout<<a<<endl;
cout<<a.to_ulong()<<endl;
a.flip();
cout<<a<<endl;
cout<<a.to_ulong()<<endl;
return 0;
}
Output:
--------test 1-----------
3
24
0
0
0
--------test 2-----------
3
24
10
10
11
--------test bitset------
8
12
000000000000
000000000001
1
111111111110
4094
#include &l
4000
t;iostream>
#include <vector>
#include <list>
#include <deque>
using namespace std;
class A{
public:
A(){cout<<"Constructor A"<<endl;}
A(A &){cout<<"Copy constructor A"<<endl;}
A(const A &){cout<<"Copy constructor A"<<endl;} //注意,对于容器,形参必须要有const关键字,否则编译会报错
};
int main(){
A aa;
cout<<"------test vector-------"<<endl;
vector<A> svec(3,aa); //容器中存放的是元素的copy
cout<<"------test list-------"<<endl;
list<A> ilist(3,aa);
}
Constructor A
------test vector-------
Copy constructor A
Copy constructor A
Copy constructor A
------test list-------
Copy constructor A
Copy constructor A
Copy constructor A
函数
#include <iostream>
#include <pthread.h>
#include <stdio.h>
#include <vector>
using namespace std;
int main(){
string s1="123";
string s2=s1+"456";//one new address to store s2
cout<<"s1 addr:"<<&s1<<" value:"<<s1<<endl;
cout<<"s2 addr:"<<&s2<<" value:"<<s2<<endl;
s1=s1+"789";
cout<<"s1 addr:"<<&s1<<" value:"<<s1<<endl;//address has no change
return 0;
}
Output:
s1 addr:0x22ac40 value:123
s2 addr:0x22ac30 value:123456
s1 addr:0x22ac40 value:123789
内存
#include <iostream>
#include <string>
#include <vector>
#include <bitset>
using namespace std;
class A{
public:
A(){cout<<"A default constructor"<<endl;}
};
int main()
{
cout<<"------test class-------"<<endl;
A *a = new A[5]; // will call default constructor to initialization, if no default constructor will raise compile error.
cout<<"------test inner type-----"<<endl;
int *b = new int [3](); //initialize
for(int i=0; i<3; i++)
{
cout<<b[i]<<endl;
}
cout<<"------test const array------"<<endl;
const string *c = new const string[2];
cout<<"str1:"<<c[0]<<endl;
cout<<"str2:"<<c[1]<<endl;
return 0;
}
Output:
------test class-------
A default constructor
A default constructor
A default constructor
A default constructor
A default constructor
------test inner type-----
0
0
0
------test const array------
str1:
str2:
STL
#include <iostream>
#include <string>
#include <vector>
#include <bitset>
using namespace std;
int main()
{
cout<<"--------test 1-----------"<<endl;
vector<int> v(3);
cout<<v.size()<<endl;
cout<<sizeof(v)<<endl;
for(vector<int>::iterator iter = v.begin(); iter != v.end(); ++iter)
{
cout<<*iter<<endl;
}
cout<<"--------test 2-----------"<<endl;
vector<int> v2(2,10);
v2.push_back(11);
cout<<v2.size()<<endl;
cout<<sizeof(v2)<<endl;
for(vector<int>::const_iterator iter = v2.begin(); iter != v2.end(); ++iter)
{
cout<<*iter<<endl;
}
cout<<"--------test bitset------"<<endl;
bitset<12> a;
cout<<sizeof(a)<<endl;
cout<<a.size()<<endl;
cout<<a<<endl;
a.set(0);
cout<<a<<endl;
cout<<a.to_ulong()<<endl;
a.flip();
cout<<a<<endl;
cout<<a.to_ulong()<<endl;
return 0;
}
Output:
--------test 1-----------
3
24
0
0
0
--------test 2-----------
3
24
10
10
11
--------test bitset------
8
12
000000000000
000000000001
1
111111111110
4094
#include &l
4000
t;iostream>
#include <vector>
#include <list>
#include <deque>
using namespace std;
class A{
public:
A(){cout<<"Constructor A"<<endl;}
A(A &){cout<<"Copy constructor A"<<endl;}
A(const A &){cout<<"Copy constructor A"<<endl;} //注意,对于容器,形参必须要有const关键字,否则编译会报错
};
int main(){
A aa;
cout<<"------test vector-------"<<endl;
vector<A> svec(3,aa); //容器中存放的是元素的copy
cout<<"------test list-------"<<endl;
list<A> ilist(3,aa);
}
Constructor A
------test vector-------
Copy constructor A
Copy constructor A
Copy constructor A
------test list-------
Copy constructor A
Copy constructor A
Copy constructor A
函数
#include <iostream> #include <string> using namespace std; class A{ public: void f(){cout<<"Hello A"<<endl;} public:~A(){cout<<"A destruct."<<endl;} }; class B:public A{ public: void f(){cout<<"Hello B"<<endl;} public: ~B(){cout<<"B destruct."<<endl;} }; int main() { A *pa = new B(); pa->f(); //析构函数不为virtual则不会析构派生类 delete pa; return 0; } Output: Hello A A destruct. | #include <iostream> #include <string> using namespace std; class A{ public: virtual void f(){cout<<"Hello A"<<endl;} public:~A(){cout<<"A destruct."<<endl;} }; class B:public A{ public: void f(){cout<<"Hello B"<<endl;} public: ~B(){cout<<"B destruct."<<endl;} }; int main() { A *pa = new B(); pa->f(); delete pa; return 0; } Output: Hello B A destruct. |
#include <iostream> #include <string> using namespace std; class A{ public: virtual void f(){cout<<"Hello A"<<endl;} public: virtual ~A(){cout<<"A destruct."<<endl;} }; class B:public A{ public: void f(){cout<<"Hello B"<<endl;} public: ~B(){cout<<"B destruct."<<endl;} }; int main() { A *pa = new B(); pa->f(); delete pa; return 0; } Output: Hello B B destruct. A destruct. | #include <iostream> #include <string> using namespace std; class A{ public: void f(){cout<<"Hello A"<<endl;} public: ~A(){cout<<"A destruct."<<endl;} }; class B:public A{ public: void f(){cout<<"Hello B"<<endl;} public: ~B(){cout<<"B destruct."<<endl;} }; int main() { B *pa = new B(); pa->f(); delete pa; return 0; } Output: Hello B B destruct. A destruct. |
#include <iostream> #include <string> #include <vector> using namespace std; class A{ public: virtual void f(){cout<<"Hello A"<<endl;} public: virtual ~A(){cout<<"A destruct."<<endl;} }; class B:virtual public A{ public: void f(){cout<<"Hello B"<<endl;} public: ~B(){cout<<"B destruct."<<endl;} }; class C:virtual public A{ public: void f(){cout<<"Hello C"<<endl;} public: ~C(){cout<<"C destruct."<<endl;} }; class D:public B,public C{ public: void f(){cout<<"Hello D"<<endl;} public: ~D(){cout<<"D destruct."<<endl;} }; int main() { A *pa = new D(); pa->f(); delete pa; return 0; } Output: Hello D D destruct. C destruct. B destruct. A destruct. | #include <iostream> #include <string> #include <vector> using namespace std; class A{ public: virtual void f(){cout<<"Hello A"<<endl;} public: A(){cout<<"A default constructor"<<endl;} public: A(const A& a){cout<<"A copy constructor"<<endl;} public: A& operator=(const A&a){cout<<"A assignment constructor"<<endl;} }; class B:virtual public A{ public: void f(){cout<<"Hello B"<<endl;} public: B(){cout<<"B default constructor"<<endl;} public: B(const B& a){cout<<"B copy constructor"<<endl;} public: B& operator=(const B&a){cout<<"B assignment constructor"<<endl;} }; class C:virtual public A{ public: void f(){cout<<"Hello C"<<endl;} public: C(){cout<<"C default constructor"<<endl;} public: C(const C& a){cout<<"C copy constructor"<<endl;} public: C& operator=(const C&a){cout<<"C assignment constructor"<<endl;} }; class D:public B,public C{ public: void f(){cout<<"Hello D"<<endl;} public: D(){cout<<"D default constructor"<<endl;} public: D(const D& a){cout<<"D copy constructor"<<endl;} public: D& operator=(const D&a){cout<<"D assignment constructor"<<endl;} }; void f(D d){} int main() { D a; D c; c = a; cout<<"start to call function"<<endl; f(a); return 0; } Output: A default constructor B default constructor C default constructor D default constructor A default constructor B default constructor C default constructor D default constructor D assignment constructor //赋值函数是不会递归调用的 start to call function A default constructor B default constructor C default constructor D copy constructor //拷贝构造函数是不会递归调用的 |
#include <iostream> #include <string> #include <vector> using namespace std; namespace wugk{ void f(); } namespace wugk{ void g(){cout<<"namespace g()"<<endl;} } void f(){cout<<"global f()"<<endl;} void wugk::f(){cout<<"namespace f()"<<endl;} int main() { f(); wugk::f(); wugk::g(); return 0; } Output: global f() namespace f() namespace g() | #include <iostream> #include <pthread.h> #include <stdio.h> class A{ public: //如果没有函数后边的const,则程序编译会报错 //但如果形参是引用或指针,则可以 void f(const int a)const{cout<<"const int a"<<endl;} void f(int a){cout<<"int a"<<endl;} }; int main(){ A a; a.f(1); const A *b = new A(); b->f(1); return 0; } Output: int a const int a |
#include <iostream> #include <pthread.h> #include <stdio.h> class A{ public: void f(const int& a){cout<<"const int a"<<endl;} void f(int &a){cout<<"int a"<<endl;} }; typedef void (A::*PF)(const int &a); int main(){ PF pl=&A::f; A a; (a.*pl)(1); return 0; } Output: const int a |
#include <iostream> #include <pthread.h> #include <stdio.h> #include <vector> using namespace std; int main(){ vector<string> vt; string s = "123"; vt.push_back(s); vt[0][0]='2'; vector< vector<string> > vt2; vt2.push_back(vt); vt2[0][0][0]='3'; cout<<"string addr:"<<&s<<" str:"<<s<<endl; cout<<"vt addr:"<<&(vt[0])<<" str:"<<vt[0]<<endl; cout<<"vt2 addr:"<<&(vt2[0][0])<<" str:"<<vt2[0][0]<<endl; return 0; } Output://vector 存放的是数据的copy string addr:0x22ac30 str:123 vt addr:0x20010360 str:223 vt2 addr:0x20010398 str:323 |
#include <pthread.h>
#include <stdio.h>
#include <vector>
using namespace std;
int main(){
string s1="123";
string s2=s1+"456";//one new address to store s2
cout<<"s1 addr:"<<&s1<<" value:"<<s1<<endl;
cout<<"s2 addr:"<<&s2<<" value:"<<s2<<endl;
s1=s1+"789";
cout<<"s1 addr:"<<&s1<<" value:"<<s1<<endl;//address has no change
return 0;
}
Output:
s1 addr:0x22ac40 value:123
s2 addr:0x22ac30 value:123456
s1 addr:0x22ac40 value:123789
内存
机器:64位SUSE char 1B short 2B int 4B float 4B double 8B long 8B longdouble 16B 指针 8B | 机器:32位Windows char 1B short 2B int 4B float 4B double 8B long 4B longdouble 12B 指针 4B |
class A{ }; //size: 1 | class A{ public: int i; };//size: 4 | class A{ public: int i; public: short j; };//size:8 | class A{ public: int i; public: short j; public: short k; };//size:8 | class A{ public: short i; public: int j; public: short k; };//size:12 |
class A { longdouble i; virtualvoid f() {}; }; //size:32 | class A { int i; virtualvoid f(){}; }; //size:16 | class A{ staticint i; };//size:1 | class A{ public: short m; public: int k; public: long n; public: virtual voidf(){}; };//size: 24 | class A{ public: short m; public: int k; public: long doublen; };//size:32 |
class B {}; class A:public B{}; //size: B:1 A:1 | class B {}; class A:virtual public B{}; //size: B:1 A:8 | class B{ public: short i; }; class A:public B{ }; //size: B:2 A:2 | class B{ public: short i; }; class A:public B{ public: int j; public: short k; }; //size: B:2 A:12 | class B{ public: int i; }; class A:virtual public B{ public: int j; public: short k; }; //size: B:4 A:24 |
class B{ public: int i; public: virtual void f(){}; }; class A:virtual public B{ public: int j; public: short k; }; //size:A:32, B:16 | class C{ public: int j; }; class B{ public: short i; }; class A:public B,public C{ public: short k; }; //size: A:12 | class C{ public: short j; }; class B{ public: short i; }; class A:public B,public C{ public: int k; }; //size: A:8 | class C{ public: short j; }; class B{ public: short i; }; class A:virtual publicB,public C{ public: int k; }; //size: A:24,B:2,C:2 | class C{ public: short j; }; class B{ public: short i; }; class A:virtual publicB,virtual public C{ public: int k; }; //size: A:16,B:2,C:2 |
class C{ public: short j; public: virtual void f(){}; }; class B:virtual public C{ public: int i; public: virtual void f(){}; }; class A:virtual public B{ public: short k; public: virtual void f(){}; }; //size: A:48, B:32, C:16 | class C{ public: int j; }; class B:virtual public C{ public: short i; }; class A:virtual public C{ public: short k; }; class D:public A,public B{ public:short m; }; //size:A:16, B:16, C:4, D:32 | A: B: | D: |
class E{ public: int i; virtualvoid f(){}; }; class F{ public: int j; E e; }; //size: F:24 | F: | class E{ public: int i; virtualvoid f(){}; }; class F:public E{ public: int j; }; //size: F:16 |
相关文章推荐
- 构造、析构、继承对对象的影响
- 构造、析构、继承对对象的影响
- 构造、析构、继承对对象的影响
- 构造、析构、继承对对象的影响
- 构造、析构、继承对对象的影响
- 构造、析构、继承对对象的影响
- 构造、析构、继承对对象的影响
- 构造、析构、继承对对象的影响
- 构造、析构、继承对对象的影响
- 构造、析构、继承对对象的影响
- 构造、析构、继承对对象的影响
- 构造、析构、继承对对象的影响
- 构造、析构、继承对对象的影响
- 构造、析构、继承对对象的影响
- 构造、析构、继承对对象的影响
- 构造、析构、继承对对象的影响
- 构造、析构、继承对对象的影响
- 构造、析构、继承对对象的影响
- 构造、析构、对象与继承
- c++子类对象构造与析构的顺序对多态性的影响