- , . , :
struct Student{
int semhours;//public
char subj; }
Student s={0,c };//
, . - , . . - , , . (inline) . :
#include<iostream.h> // 22
class Student{
int semhours;
char subj;
public:
Student() //inline- 1
{semhours=0; subj='A';}
Student(int, char); // 2
};
Student::Student(int hours,char g)// 2
{semhours=hours; subj=g;}
int main(){
student s(100,'A'); // 2
student s1[5]; //1 5
return 0;
}
, s s1, Student().
, . , new . , ++ .
, , , ~. . , .
, - , . , , . , . , , , . , , , . . :
Class_type B=A;
. B , . , , , . .
|
|
:
_ (const _ &ob) {
/* ob*/
}
ob .
.
/* "" . - , */
#include <iostream.h> // 23
#include <stdlib.h>
#include <conio.h>
class Array {
int *p;
int size;
public:
Array (int s) { cout << "constructor1 \n";
size=s;
p=new int[size];
if(!p) exit(1);
}
~Array() {delete [ ] p;}
Array(const Array &a);//
void put(int i, int j) {
if(i>=0 && i<size) p[i]=j;
}
int get(int i) {
return p[i]; }
};
/* . , p.*/
Array:: Array(const Array &a) {
p=new int[a.size]; //
if(!p) exit(1);
for(int i=0;i<a.size;i++)
p[i]=a.p[i];//
cout << "constructorcopy2\n";}
int main(){
Array y(5); //
for(int i=0;i<5;i++){
y.put(i, i+5); //
cout << y.get(i); // num
}
cout << "\n";
Array x=y; /* x , */
// - : Array x(y);
y=x;// !!!
while(!kbhit());
return 0;
}
:
constructor1
constructorcpy2
y . , x.p, y x. y x . , , . . y=x .
- show():
#include <iostream.h> // 24
#include <stdlib.h>
#include <conio.h>
class Strtype {
char *p;
int l;
public:
Strtype(char *s='\0') //
{ l=strlen(s);
p=new char[l+1];
if(!p) {cout << " \n";exit(1); }
strcpy(p, s);
cout<<"constr1\n";
}
Strtype(const Strtype &o)//
{ l=strlen(o.p);
p=new char[l+1];//
if(!p) {cout << " \n";exit(1);}
strcpy(p, o.p); //
cout<<"constrcpy\n";
}
~Strtype() {delete [ ] p;cout<<"destr\n";} //
char *get() {return p;}
};
void show(Strtype x)
{ char *s;
s=x.get();cout << s << "\n";}
int main(){
Strtype a("Hello"), b("There");//constr1,constr1
|
|
show(a);// constrcpy,Hello,destr
show(b);// constrcpy,There,destr
while(!kbhit());
return 0;
}
show() , ., , .