- 构造函数
#include <iostream>
using namespace std;
class Line
{
public:
void setLength( double len );
double getLength( void );
Line();
private:
double length;
};
Line::Line(void)
{
cout << "Object is being created" << endl;
}
void Line::setLength( double len )
{
length = len;
}
double Line::getLength( void )
{
return length;
}
int main( )
{
Line line;
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;
Line line1;
return 0;
}
- 拷贝构造函数
#include <iostream>
using namespace std;
class Line
{
public:
int getLength( void );
Line( int len );
Line( const Line &obj);
~Line();
private:
int *ptr;
};
Line::Line(int len)
{
cout << "调用构造函数" << endl;
ptr = new int;
*ptr = len;
}
Line::Line(const Line &obj)
{
cout << "调用拷贝构造函数并为指针 ptr 分配内存" << endl;
ptr = new int;
*ptr = *obj.ptr;
}
Line::~Line(void)
{
cout << "释放内存" << endl;
delete ptr;
}
int Line::getLength( void )
{
return *ptr;
}
void display(Line obj)
{
cout << "line 大小 : " << obj.getLength() <<endl;
}
void fun()
{
cout<<"not use Line"<<endl;
}
int main( )
{
Line line1(10);
Line line2 = line1;
cout<<endl<<"dispaly_line1"<<endl;
display(line1);
cout<<endl<<"dispaly_line2"<<endl;
display(line2);
cout<<"over"<<endl;
fun();
return 0;
}
- 类01
#include <iostream>
using namespace std;
class Box
{
public:
double length;
double breadth;
double height;
double get(void);
void set( double len, double bre, double hei );
};
double Box::get(void)
{
return length * breadth * height;
}
void Box::set( double len, double bre, double hei)
{
length = len;
breadth = bre;
height = hei;
}
int main( )
{
Box Box1;
Box Box2;
Box Box3;
double volume = 0.0;
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Box1 的体积:" << volume <<endl;
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Box2 的体积:" << volume <<endl;
Box3.set(16.0, 8.0, 12.0);
volume = Box3.get();
cout << "Box3 的体积:" << volume <<endl;
return 0;
}
- 类02
#include <iostream>
using namespace std;
class Box
{
public:
double length;
double breadth;
double height;
double getVolume(void);
void setLength( double len );
void setBreadth( double bre );
void setHeight( double hei );
};
double Box::getVolume(void)
{
return length * breadth * height;
}
void Box::setLength( double len )
{
length = len;
}
void Box::setBreadth( double bre )
{
breadth = bre;
}
void Box::setHeight( double hei )
{
height = hei;
}
int main( )
{
Box Box1;
Box Box2;
double volume = 0.0;
Box1.setLength(6.0);
Box1.setBreadth(7.0);
Box1.setHeight(5.0);
Box2.length=12.0;
Box2.breadth=13.0;
Box2.height=10.0;
volume = Box1.getVolume();
cout << "Box1 的体积:" << volume <<endl;
volume = Box2.getVolume();
cout << "Box2 的体积:" << volume <<endl;
return 0;
}
- 类03_private
#include <iostream>
using namespace std;
class Box
{
public:
double length;
void setWidth( double wid );
double getWidth( void );
private:
double width;
};
double Box::getWidth(void)
{
return width ;
}
void Box::setWidth( double wid )
{
width = wid;
}
int main( )
{
Box box;
box.length = 10.0;
cout << "Length of box : " << box.length <<endl;
box.setWidth(10.0);
cout << "Width of box : " << box.getWidth() <<endl;
return 0;
}
- 类04_protected
#include <iostream>
using namespace std;
class Box
{
protected:
double width;
};
class SmallBox:Box
{
public:
void setSmallWidth( double wid );
double getSmallWidth( void );
};
double SmallBox::getSmallWidth(void)
{
return width ;
}
void SmallBox::setSmallWidth( double wid )
{
width = wid;
}
int main( )
{
SmallBox box;
box.setSmallWidth(5.0);
cout << "Width of box : "<< box.getSmallWidth() << endl;
return 0;
}
- 析构函数
#include <iostream>
using namespace std;
class Line
{
public:
void setLength( double len );
double getLength( void );
Line();
~Line();
private:
double length;
};
Line::Line(void)
{
cout << "Object is being created" << endl;
}
Line::~Line(void)
{
cout << "Object is being deleted" << endl;
}
void Line::setLength( double len )
{
length = len;
}
double Line::getLength( void )
{
return length;
}
int main( )
{
Line line;
line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<endl;
line.setLength(7.0);
cout << "Length of line : " << line.getLength() <<endl;
return 0;
}
- 友元函数
#include <iostream>
using namespace std;
class Box
{
double width;
public:
friend void printWidth( Box box );
void setWidth( double wid );
};
void Box::setWidth( double wid )
{
width = wid;
}
void printWidth( Box box )
{
cout << "Width of box : " << box.width <<endl;
}
int main( )
{
Box box;
box.setWidth(10.0);
printWidth( box );
return 0;
}
|