(V
isu
al
C+
+
6.
0
10 C++
10 C++
(V
isu
al
C+
+
6.
0
10.2
10.3
10.5
10.4
10.1 C++
(V
isu
al
C+
+
6.
0
10.1.2 C++
10.1.1 C++
10.1 C++
10.1.1 C++
(V
isu
al
C+
+
6.
0 2,C++(2)
3,C++(3)
1,C++(1)
10.1l10_1.cpp
#include<iostream.h>
class rectangle //
{ public:
rectangle(float len,float wid) //
{ length=len;
width=wid;
}
float GetArea(); //,
float GetPerimeter(); //,
~rectangle(){} //
private:
float length; //
float width;
};
10.1.1 C++
(V
isu
al
C+
+
6.
0
float rectangle::GetArea() //
{ return length*width;
}
float rectangle::GetPerimeter() //
{ return 2*(length+width);
}
void main()
{ float l,w;
cout<<":";
cin>>l>>w;
rectangle x(l,w); //
cout<<x.GetArea()<<endl;
cout<<x.GetPerimeter()<<endl; //
}
10.1.1 C++
(V
isu
al
C+
+
6.
0
10.1.1 C++
(V
isu
al
C+
+
6.
0
1
56< CR >,
30
22
10.2l10_2.cpp
#include<iostream.h>
class rectangle //
{ public:
void InitRect(float len,float wid) //
{ length=len;
width=wid;
}
float GetArea();
float GetPerimeter();
private,//
float length;
float width;
};
float rectangle::GetArea() //
{ return length*width;}
10.1.1 C++
(V
isu
al
C+
+
6.
0
float rectangle::GetPerimeter() //
{ return 2*(length+width);
}
class square:public rectangle //()
{public:
void InitSquare(float b){InitRect(b,b);} //(
}; //)
void main()
{ square x; //
x.InitSquare(8); //
cout<<x.GetArea()<<endl; //cout<<x.GetPerimeter()<<endl; //
} //(GetPerimeter)
10.1.1 C++
(V
isu
al
C+
+
6.
0
10.1.1 C++
(V
isu
al
C+
+
6.
0
28
64
32
10.3“+”l10_3.cpp
#include<iostream.h>
class rectangle //
{public:
rectangle(float len=0,float wid=0) //
{ length=len;
width=wid;
}
float GetArea(){return length*width;} //()
rectangle operator +(rectangle a2) //"+"
{ rectangle a; //
a.length=length;
a.width=width+a2.GetArea()/length;
return rectangle(a.length,a.width);
}
10.1.1 C++
(V
isu
al
C+
+
6.
0
private,//
float length;
float width;
};
void main()
{ rectangle x(5,9),y(5,6),z; //
cout<<":"<<x.GetArea()<<endl;
cout<<":"<<y.GetArea()<<endl;
z=x+y; //
cout<<":"<<z.GetArea()<<endl;
}
10.1.1 C++
(V
isu
al
C+
+
6.
0
3,+”
:45
:30
:75
10.1.2 C++
(V
isu
al
C+
+
6.
0
2080C++
Object-Oriented programming OOP
10.1.2 C++
(V
isu
al
C+
+
6.
0
C++CC++CC++CCC++
C++CC++CC++
(V
isu
al
C+
+
6.
0
10.2.2
10.2.1
10.2
10.2.3
10.2.4
10.2.5
10.2.6
10.2.1
(V
isu
al
C+
+
6.
0
1,
10.1
10.2.1
(V
isu
al
C+
+
6.
0
10.2.2
(V
isu
al
C+
+
6.
0
10.2.2
(V
isu
al
C+
+
6.
0
class
{
public:
/**/
protected:
private:
}
10.2.2
(V
isu
al
C+
+
6.
0
class
public
Protected
Private
10.2.2
(V
isu
al
C+
+
6.
0
::()
{
}
::
C++
void
10.2.2
(V
isu
al
C+
+
6.
0
10.2.2
(V
isu
al
C+
+
6.
0
~”
10.2.2
(V
isu
al
C+
+
6.
0
10.4l10_4.cpp
#include<iostream.h>
class A //A
{ public:
A()
{ cout<<,<<endl; //
}
void disp() //
{ cout<<,<<endl;
}
~A() //
{ cout<<""<<endl;
}
};
A dispdisp
10.2.2
(V
isu
al
C+
+
6.
0
void main()
{ A a; //
a.disp(); //
//
}
10.2.3
(V
isu
al
C+
+
6.
0
CADC++
C++new
new
new ()
int *p;
p=new int(100); //
rectangle *r;
r=new rectangle(5,6); //
10.2.3
(V
isu
al
C+
+
6.
0
10.2.3
(V
isu
al
C+
+
6.
0
new []
0
int *p;
p=new int[10];
new [1][2]……
int (*p)[5];
p=new int[10][5];
10.2.3
(V
isu
al
C+
+
6.
0
deletenew
delete
int *p;
p=new int(100);
delete p;
new
delete []
int *p;
p=new int[10];
delete []p;
10.2.3
(V
isu
al
C+
+
6.
0
10.5l10_5.cpp
#include<iostream.h>
class rectangle // {public:
rectangle(float len,float wid) //
{ length=len;
width=wid;
}
float GetArea(); //
private:
float length;
float width;
};
10.2.3
(V
isu
al
C+
+
6.
0
float rectangle::GetArea() // { return
length*width;
}
void main()
{ rectangle *r; //rectangler
r=new rectangle(10,5); //rectangle
cout<<r->GetArea()<<endl;
delete r; //
}
rectanglerectanglernewrectanglerectanglerdelete
10.2.4
(V
isu
al
C+
+
6.
0 2,
3,
1,
10.2.4
(V
isu
al
C+
+
6.
0
10.2.4
(V
isu
al
C+
+
6.
0
friend
class
{
friend ();
}
10.2.4
(V
isu
al
C+
+
6.
0
10.6
l10_6.cpp
#include <iostream.h>
#include <math.h>
class Point //Point()
{public:
Point(double xx,double yy) //
{x=xx;
y=yy;}
void Getxy(); //
friend double Distance(Point &a,Point &b);//
private:
double x,y; //()
};
void Point::Getxy() //
{cout<<“(”<<x<<“,”<<y<<“)”; //
}
double Distance(Point &a,Point &b) //
{ double dx = a.x - b.x; //
double dy = a.y - b.y;
return sqrt(dx*dx+dy*dy); //
}
void main()
{ Point p1(3.0,4.0),p2(6.0,8.0); //Point
p1.Getxy(); //
p2.Getxy();
cout<<“\nThe istance is,<<Distance(p1,p2)<<endl;
//
}
10.2.4
(V
isu
al
C+
+
6.
0
10.2.4
(V
isu
al
C+
+
6.
0
friendPointGetxy
(3,4)(6,8)
The distance is 5
10.2.4
(V
isu
al
C+
+
6.
0
class A
{
A
friend class B;
A
}
10.7l10_7.cpp
#include<iostream.h>
class A //A
{ public:
A(int xx,int yy) //A
{ x=xx;
y=yy;
}
friend class B; //BA
private:
int x,y;
};
class B //B
{public:
void disp1(A s)
{cout<<"disp1Ax:"<<s.x<<endl;}
//BA
10.2.4
(V
isu
al
C+
+
6.
0
ABBAABBABdisp1disp2AAxy
disp1Ax:5
disp2Ay:9
10.2.4
(V
isu
al
C+
+
6.
0
void disp2(A s)
{ cout<<"disp2Ay:"<<s.y<<endl;}
//BA
};
void main()
{
A a(5,9); //A
B b; //B
b.disp1(a); //B
b.disp2(a);
}
10.2.5
(V
isu
al
C+
+
6.
0 template <typename >
10.2.5
(V
isu
al
C+
+
6.
0
10.8l10_8.cpp
#include <iostream.h>
template <typename T>
T max(T x,T y) //
{ return x>y?x:y;
}
void main()
{ int a=6,b=8;
cout<<max(a,b)<<endl;
double m=10.5,n=8.5;
cout<<max(m,n)<<endl;
}
maxmax(a,b)a,bTintmax(m,n)m,ndoubleTdouble
10.2.5
(V
isu
al
C+
+
6.
0
template <class >
10.9l10_9.cpp
#include<iostream.h>
template <class T> //class rectangle //rectangle
{public:
rectangle(T len,T wid) //
{ length=len;
width=wid;
}
T GetArea(); //,
T GetPerimeter(); //,private:
T length; //,
T width;
};
template <class T>
T rectangle<T>::GetArea() //
{ return length*width;}
10.2.5
(V
isu
al
C+
+
6.
0
template <class T>
T rectangle<T>::GetPerimeter() //
{ return 2*(length+width);}
void main()
{ rectangle<int> x(4,6); //rectangle(int)
rectangle<double> y(5.5,3.8); //rectangle(double)
cout<<“x:”<<x.GetArea()<<endl;
cout<<“x:”<<x.GetPerimeter()<<endl;
cout<<“y:"<<y.GetArea()<<endl;
cout<<"y:"<<y.GetPerimeter()<<endl;
}
x:24
x:20
y:20.9
y:18.6
10.2.5
(V
isu
al
C+
+
6.
0
#include<iostream.h>
class TDate //
{public:
TDate(int y,int m,int d); //
int IsLeapYear(); //
void show(); //
friend int Getyear(TDate date); //,
private:
int year,month,day;
};
TDate::TDate(int y,int m,int d) //
{ year=y;
month=m;
day=d;
}
10.2.6
(V
isu
al
C+
+
6.
0
int TDate::IsLeapYear()
{ return(year%4==0 && year%100!=0) || (year%400==0);}
void TDate::show()
{ cout<<year<<,<<month<<,<<day<<,<<endl;}
int Getyear(TDate date) //
{ return date.year;}
void main()
{ TDate *date; //TDate
date=new TDate(2008,10,28); //
date->show(); //
if(date->IsLeapYear())
cout<<Getyear(*date)<<,<<endl; //
else
cout<<Getyear(*date)<<“"<<endl;
delete date; //
}
10.2.6
(V
isu
al
C+
+
6.
0
10.2.6
(V
isu
al
C+
+
6.
0
CDateTDatenewdeletenewdelete
(V
isu
al
C+
+
6.
0
10.3
C++
windows
10.3.1
(V
isu
al
C+
+
6.
0
10.3.1
(V
isu
al
C+
+
6.
0
10.3.1
(V
isu
al
C+
+
6.
0
10.3.2
(V
isu
al
C+
+
6.
0
class,
{ };
publicprotectedprivateprivate
10.3.2
(V
isu
al
C+
+
6.
0
1
publicpublicprotected
2
protectedpublicprotected
10.3.2
(V
isu
al
C+
+
6.
0
3
privatepublicprotected(:)
10.1.12InitSquareInitSquareInitRectInitRectInitSquare
void InitSquare (float b)
{ length=b;
width=b; }
10.3.2
(V
isu
al
C+
+
6.
0
10.3.3
(V
isu
al
C+
+
6.
0
10.3.3
(V
isu
al
C+
+
6.
0
{
}
10.2
10.11l10_11.cpp
#include<iostream.h>
class rectangle //()
{public:
rectangle(float len,float wid) // { length=len;
width=wid;
}
float GetArea();
float GetPerimeter();
private,//
float length;
float width;
};
float rectangle::GetArea() //
{ return length*width;
}
float rectangle::GetPerimeter() //
{ return 2*(length+width);
}
class rect:public rectangle //()
{public:
rect(float b):rectangle(b,b) //
{}
};
10.3.3
(V
isu
al
C+
+
6.
0
10.3.3
(V
isu
al
C+
+
6.
0
void main()
{ rect x(8); //
cout<<x.GetArea()<<endl; //
//(GetArea)
cout<<x.GetPerimeter()<<endl; //
//(GetPerimeter)
}
64
32
10.3.3
(V
isu
al
C+
+
6.
0
10.12l10_12.cpp
#include <iostream.h>
class color //color
{public:
void paint(){cout<<“No color.”<<endl;}
};
class green:public color //colorgreen
{public:
void paint(){cout<<"The color is green."<<endl;}
};
10.3.4
(V
isu
al
C+
+
6.
0
virtual virtual
10.3.4
(V
isu
al
C+
+
6.
0
void main()
{ color c,*p; //color
green g; //green
p=&g; //colorpgreen
c.paint();
g.paint();
p->paint();
}
10.3.4
(V
isu
al
C+
+
6.
0
cgpgreencolorpaint()
color
class color //color
{public:
virtual void paint(){cout<<"No color."<<endl;} //
};
10.3.4
(V
isu
al
C+
+
6.
0
C++C++
10.3.5
(V
isu
al
C+
+
6.
0
basestudentclasses
10.3.5
(V
isu
al
C+
+
6.
0
10.14l10_14.cpp#include<iostream.h>
#include<string.h>
class base //base
{ private:
char str1[20];
char str2[10];
public:
base(){}
base(char s1[],char s2[])
{ strcpy(str1,s1);
strcpy(str2,s2);
}
void show1()
{ cout<<str1;
}
10.3.5
(V
isu
al
C+
+
6.
0
void show2()
{ cout<<str2;
}
};
class classes:public base //
{private:
char cname[20]; //
public:
classes(){}
classes(char cna[],char cweek[],char ctea[]):base(cweek,ctea)
//
{strcpy(cname,cna);}
void show()
{ cout<<cname<<“(”;
show1(); //(,)
cout<<“,”;
show2();
cout<<“)”;
}
};
class student:public base //
{ private:
classes cls[3]; //3
int max;
public:
student(){}
student(char sname[],char sno[]):base(sname,sno){max=0;}
10.3.5
(V
isu
al
C+
+
6.
0
10.3.5
(V
isu
al
C+
+
6.
0
void selectcls(classes c) //
{ cls[max]=c;
max++;
}
void showcls() //
{ cout<<,;
show1();
cout<<"(";
show2();
cout<<")"<<endl<<":"<<endl;
for(int i=0;i<max;i++)
{ cout<<i+1<<":";
cls[i].show();
cout<<endl;
}
}
};
10.3.5
(V
isu
al
C+
+
6.
0
void main()
{ student stu("","12345678");
classes cls1("","","");
classes cls2("C++","","");
classes cls3("","","");
stu.selectcls(cls1);
stu.selectcls(cls2);
stu.selectcls(cls3);
stu.showcls();
}
(V
isu
al
C+
+
6.
0 10.4.2
10.4.1
10.4
10.4.3
10.4.1
(V
isu
al
C+
+
6.
0
C++ C++
C++@
10.4.1
(V
isu
al
C+
+
6.
0
operator@ +”operator+
10.1.13 +”operatore
10.1.13 +” +”C++ +”
10.4.2
(V
isu
al
C+
+
6.
0
1) C++
2)
3)
.” *,:”?:”sizeof
(V
isu
al
C+
+
6.
0
operator ()
{
}
friend operator ()
{
}
10.4.2
10.4.2
(V
isu
al
C+
+
6.
0
operatorC++
C++
C++
1
2
3
10.4.3
(V
isu
al
C+
+
6.
0
10.4.3
(V
isu
al
C+
+
6.
0
int *p = 10;
int *p = (int*)10; // 10
C++
10.4.3
(V
isu
al
C+
+
6.
0
1
10.15l10_15.cpp
#include <iostream.h>
class A //A
{public:
A(double x){m=x;}
void print(){cout<<m<<endl;}
private:
double m;
};
10.4.3
(V
isu
al
C+
+
6.
0
void main()
{ A a(5);
a=100; //A
a.print();
}
a=100100a100doubledoubleAa
10.4.3
(V
isu
al
C+
+
6.
0
2
operatore
class
{ public:
operatore //
}
10.4.3
(V
isu
al
C+
+
6.
0
10.16l10_16.cpp
#include <iostream.h>
class R //R
{public:
R(int w,int l)
{ wid = w;
len = l;
}
operator int(); //
private:
int wid,len;
};
R::operator int() //
{ return wid*len;
}
void main()
{ R r(5,8);
int s = 8;
s+=r; //R
cout<<s<<endl;
}
sintrRoperator int()RRintRoperator int()s+=r
48
10.4.3
(V
isu
al
C+
+
6.
0
(V
isu
al
C+
+
6.
0
10.5
PointCirclePointCirclePoint
+” -”
10.17l10_17.cpp
#include<iostream.h>
#include<math.h>
class Point //Point
{private:
double x,y; //()
public:
Point(){} //
Point(double px,double py) //
{x=px;y=py;}
void show() //()
{cout<<“(”<<x<<“,”<<y<<“)”;}
friend double operator-(Point p1,Point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
} //“-”
};
10.5
(V
isu
al
C+
+
6.
0
class Circle:public Point //PointCircle
{private:
Point p;
double r; //
public:
Circle(double px,double py,double cr):Point(px,py)
{ //
p=Point(px,py);
r=cr;
}
void show() //
{ cout<<"[p";
Point::show();
cout<<","<<r<<"]";
}
10.5
(V
isu
al
C+
+
6.
0
Point GetCenter() //()
{ return p;}
friend double operator+(Circle c1,Circle c2)
{ return c1.r+c2.r; //“+”,
}
};
void Crelation(Circle cr1,Circle cr2)
{ //
if((cr1.GetCenter()-cr2.GetCenter())>(cr1+cr2))
cout<<""<<endl;
if((cr1.GetCenter()-cr2.GetCenter())==(cr1+cr2))
cout<<""<<endl;
if((cr1.GetCenter()-cr2.GetCenter())<(cr1+cr2))
cout<<""<<endl;
}
10.5
(V
isu
al
C+
+
6.
0
void main()
{ Point p1(1,1); //Pointp1
Point p2(4,5); //Pointp2
cout<<“p1p2:”<<endl;
p1.show(); //p1
cout<<“->”;
p2.show(); //p2
cout<<“=”<<p1-p2<<endl; //
Circle c1(0,0,1); //Circlec1
Circle c2(3,0,2); //Circlec2
cout<<“c1c2:”<<endl;
c1.show(); //c1
cout<<"->";
c2.show(); //c2
10.5
(V
isu
al
C+
+
6.
0
cout<<“=”<<c1.GetCenter()-c2.GetCenter()<<endl;
//
cout<<“:”<<c1+c2<<endl;
cout<<“:”;
Crelation(c1,c2);
}
10.5
(V
isu
al
C+
+
6.
0