- .
, .
. \ .
, , .
class A
{
public:
virtual void fun(){ cout << "fun from A" << endl;}
};
class B1:public A
{
public:
void fun(){ cout << "fun from B1" << endl;}
};
class B2:public A
{
public:
void fun(){ cout << "fun from B2" << endl;}
};
int main(){
A *p, obA;
B1 obB1;
B2 obB2;
p=&obA; //
p->fun();
p=&obB1; p->fun();
p=&obB2; p->fun();
return 0;
}
- .
Hjj
. .
, , . . . .. , , . .: ( ).
~ , , .
class Man
{
public:
char *FIO;
int year;
Man(char *s = "\0", int y = 0);
void print();
~Man();
};
Man::Man(char *s, int y)
{
int len = strlen(s);
FIO = new char[len + 1];
strcpy(FIO, s);
year=y;
}
Man::~Man()
{
delete [] FIO;
}
void Man::print()
{
cout<<FIO<<'\t'<<year<<endl;
}
int main(){
Man ob("a",5);
ob.print();
return 0;
}
.
, , , , , , . , .
class A
{
private:
char *str;
int len;
public:
A();
A(const A&);
~A();
};
A::A()
{
len = 20;
str = new char[len + 1];
}
A::A(A const &obj1) // copy from obj1 to obj
{
this->len = obj1.len;
str=new char[len+1];
strcpy(str,obj1.str);
}
A::~A()
{
delete []str;
}
void fun(A obj1)
{
cout<< "qwert" << endl;
}
void main()
{
A obj;
fun(obj);
}
explicit.
|
|
.. .
class Mass
{ private:
int n;
int *M;
public:
class Size
{
private:
int count;
public:
Size(int count1):count(count1){}
int size_mass() const {return count;}
};
Mass(int n, int m)
{this->n=n;
M=new int[this->n];
for(int i=0; i < this->n; i++)
M[i] = m;
}
Mass(Size s)
{
n = s.size_mass();
M = new int[n];
}
~Mass()
{
delete []M;
}
};
void main()
{
Mass ob(1);
}
ob Mass(int). . int Size, Size . .
_____________________
stown, stown(double k)
- :
stown myCat;
myCat = 19.333 // ! stown(double) 19.333 stown.
explicit ! ))))
this.
this-; this .. ; this ; this ; this - : this .
: .
class Mass
{int a[3];
public:
Mass sort();
Mass *input();
void out();
};
Mass Mass::sort()
{ for(int i = 0; i < 2; i++)
for(int j = i; j < 3; j++)
if (a[i]>a[j])
{ a[i] = a[i] + a[j]; a[j] = a[i] - a[j]; a[i] = a[i] - a[j]; }
return *this; //
}
Mass *Mass::input()
{
for (int i = 0; i < 3; i++) cin>>a[i];
return this; //
}
void Mass::out()
{
cout<<endl;
for(int i = 0; i<3; i++)
cout<< a[i] << ' ';
}
void main()
{
Mass o1;
o1.input()->sort().out();
}
input o1; , o1; , 1; .
.
́ ́ () , , . . . .
, . , . - . . , , , , . , , , , .
|
|
, , . , . , ( , ) .
.
class A
{ private: int a;
public:
void set_a(int num);
int get_a();
};
void A::set_a(int num)
{ a = num;}
int A::get_a()
{ return a;}
void main()
{
A ob1, ob2;
ob1.set_a(10);
ob2.set_a(99);
//ob1.a = 10;//ERROR
cout << ob1.get_a() << endl;
cout << ob2.get_a() << endl;
}
8. / ++.
++ . ( ) − , -. . .
cin [>>_]; cin . , , .
out << data [ << data];
data − , ,
.
cout − , ,
:
cout << - ;
9. / ++.
cout << setw(10) << setfill('*') << a << endl;
setw(____)
setfil(____)
setprecision (____
int main()
{ int a=0x11, b=4, // :
c=051, d=8, //
i,j;
i=a+b;
j=c+d;
cout << i <<' ' <<hex << i <<' '<<oct << i <<' ' <<dec << i <<endl;
cout <<hex << j <<' ' << j <<' '<<dec << j <<' ' << oct << j <<endl;
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{ int a=0x11;
double d=12.362;
cout << setw(4) << a << endl;
cout << setw(10) << setfill('*') << a << endl;
cout << setw(10) << setfill(' ') << setprecision(3) << d << endl;
return 0;
}
:
********17
12.4
10. (new delete).
: .
-
. -
, heap. ,
.
|
|
calloc, malloc, realloc, free . ++
new delete.
new . :
[::] new [(_)] _ [(_)]
[::] new [(_)] (_) [(_)]
new :
new _ new (_)
new _, -
. :
char *str; // str char
str=new char; // char
str=new (char);
class A
{ int i; // -
public:
A(){} //
~A(){} //
};
int main()
{ A *a,*b; //
float *c,*d; // float
a=new A; //
b=new A[3]; //
c=new float; // float
d=new float[4]; // float
delete a; // ,
delete [] b; // ,
delete c; // float
delete [] d; // float
}
delete
, .
delete[] -
. -
, .
. , new -
NULL.
.
. , \ .
. ( + ).
.
class A
{
int i;
public:
A(int ii = 0) {i = ii;}
void show() {cout<<i<<endl;}
};
class B:A
{
int j;
public:
B(int ii = 0, int jj = 1):A(ii) {j=jj;}
void show()
{cout<<j<<endl;
A::show();
}
};
void main()
{ A obj(5);
obj.show();
B obj2(7,4);
obj.show();
}
//
5 4 7