【c++】黑马·2 类和对象——代码练习
时间:2022-08-22 13:30:01
目录
黑马p99学生类
黑马p私有103类封装属性
黑马p104立方体类全局成员函数面积体积
黑马p105点类圆关系
黑马p复制构造函数107
黑马p111列表初始化
黑马p111列表初始化
黑马p112手机人类
黑马p113静态成员
黑马p115this
黑马p116this空指针
黑马p117const类
黑马p118友元friend全局函数
黑马p119类友元
黑马p120成员函数友元
黑马p99学生类
/*黑马p学生类,属性姓名和学号,赋值,显示*/ #include #include using namespace std; class Stu { public: string name; string id; void get1() { cout << "请输入学生姓名:"; cin >> name; cout << "请输入学生学号:"; cin >> id; } void show1() { cout << "name:" << name << endl << "id:" << id << endl; } }; int main() { Stu jennie; jennie.get1(); jennie.show1(); return 0; }
黑马p私有103类封装属性
/*黑马p103类封装属性私有,名称可读可写,年龄可读不可写,情人可写不可读*/ #include #include using namespace std; class Per { string pname; int page=10; string plover; public: void getname(string name) { pname = name; } string showname() { return pname ; } int showage() { return page; } void getlover(string lover) { plover = lover; } }; int main() { Per p; p.getname("jennie"); cout<<"name:"<
黑马p104立方体全球成员函数面积
/*立方体类,求体积面积,全局、成员函数证同*/ #include using namespace std; class Cube { int cl; int cw; int ch; public: void set(int l,int w,int h) { cl = l; cw = w; ch = h; } int getl() { return cl; } int getw() { return cw; } int geth() { return ch; } int area() { return (cl * cw ch * cw cl * ch)*2; } int volumn() { return cl*cw*ch; } bool same(Cube c) { if (cl == c.getl() && cw == c.getw() && ch == c.geth()) return 1; return 0; } }; bool same(Cube c1, Cube c2) { if (c1.getl() == c2.getl() && c1.getw() == c2.getw()&& c1.geth() == c2.geth()) return 1; return 0; } int main() { Cube c1,c2; c1.set(1, 2, 3); c2.set(1, 2, 2); cout << "area:" << c1.area() << endl; cout << "volume:" << c2.volumn() << endl; if(c1.same(c2)==c2.same(c2)) cout << "same"; else cout << "no same"; if (same(c1, c2)) cout << "same"; else cout << "no same"; return 0; }
黑马p105点类圆关系
/*黑马p105点类圆关系*/ #iclude
using namespace std;
#include"circle.h"
int rel(P p,Circle c)
{
int cr = c.getr(), cx = c.getx(), cy = c.gety(), px = p.getx(), py = p.gety(), dx = px - cx, dy = py - cy, d = dx * dx - dy * dy;
if (d > cr*cr)
return 1;
else if (d < cr*cr)
return 2;
return 3;
}
int main()
{
P p,cp;
p.set(4, 4);
cp.set(3, 4);
Circle c;
c.set(cp, 2);
cout << "圆成员函数:";
if (c.rel(p) == 1)
cout << "圆外" << endl;
else if (c.rel(p) == 2)
cout << "圆内" << endl;
else if(c.rel(p) == 3)
cout << "圆上" << endl;
cout << "函数:";
if (rel(p,c) == 1)
cout << "圆外" << endl;
else if (rel(p,c) == 2)
cout << "圆内" << endl;
else if (rel(p,c) == 3)
cout << "圆上" << endl;
return 0;
}
黑马p107拷贝构造函数
/*黑马p107拷贝构造函数*/
#include
using namespace std;
class Person
{
public:
int page;
Person()
{
cout << "构造函数无参调用" << endl;
}
Person(int age)
{
page = age;
cout << "构造函数有参调用" << endl;
}
Person(const Person &p)
{
page = p.page;
cout << "拷贝构造函数调用" << endl;
}
~Person()
{
cout << "析构函数的调用" << endl;
}
};
void test()
{
cout << "括号调用:" << endl;
Person p1;
Person p2(10);
Person p3(p2);
cout << "p2的年龄是:" << p2.page<
黑马p111列表初始化
/*类里有开辟堆区(指针),拷贝构造函数要重新写,重新申请*/
/*黑马p107拷贝构造函数*/
#include
using namespace std;
class Person
{
public:
int page;
int* pheight;
Person()
{
cout << "构造函数无参调用" << endl;
}
Person(int age,int height)
{
page = age;
pheight = new int(height);
cout << "构造函数有参调用" << endl;
}
Person(const Person &p)
{
page = p.page;
pheight = new int(*p.pheight);
cout << "拷贝构造函数调用" << endl;
}
~Person()
{
if (pheight != NULL)
{
delete pheight;
pheight = NULL;
}
cout << "析构函数的调用" << endl;
}
};
void test()
{
Person p1(18,160);
Person p3(p1);
cout << "p2的年龄是:" << p1.page<<" 身高是:"<<*p1.pheight<
黑马p111列表初始化
/*类里有开辟堆区(指针),拷贝构造函数要重新写,重新申请*/
/*黑马p107拷贝构造函数*/
#include
using namespace std;
class Person
{
public:
int pa;
int pb;
int pc;
Person(int a, int b, int c) :pa(a), pb(b), pc(c)
{
cout << "构造函数调用" << endl;
}
};
int main()
{
Person p(10, 20, 30);
cout << p.pa << endl << p.pb << endl << p.pc;
return 0;
}
黑马p112手机人类
#include
#include
using namespace std;
class Phone
{
public:
string phname;
Phone(string name):phname(name)
{
cout << "phone构造函数调用" << endl;
}
};
class Person
{
public:
string pname;
Phone ph;
Person(string name,string phone) :pname(name), ph(phone)
{
cout << "构造函数调用" << endl;
}
};
int main()
{
Person p("张三","iphone");
cout << p.pname << "拿着" << p.ph.phname ;
return 0;
}
黑马p113静态成员
#include
#include
using namespace std;
class Person
{
public:
static void func()
{
//b = 0;
a = 0;
cout << "静态成员函数调用" << endl;
}
static int a;
int b;
};
void test()
{
cout << "通过创建对象来调用静态成员函数:" << endl;
Person p;
p.func();
cout << "直接调用静态成员函数:" << endl;
Person::func();
cout<
黑马p115this
/*黑马p115this*/
#include
using namespace std;
class Person
{
public:
Person(int age)
{
this->age = age;
}
Person& addage(int age)
{
this->age += age;
return *this;
}
int age;
};
int main()
{
Person p(20);
p.addage(10).addage(30);
cout << p.age;
return 0;
}
黑马p116this空指针
/*黑马p116this空指针*/
#include
using namespace std;
class Person
{
public:
void showclassname()
{
cout << "这还是Person类"<showclassname();
p->showage();
}
int main()
{
test();
return 0;
}
黑马p117const类
/*黑马p117const类*/
#include
using namespace std;
class Person
{
public:
void func1() const
{
//this->a = 100;
//this = NULL;
this->b = 100;
}
void func2()
{
cout << "普通成员函数" << endl;
}
int a;
mutable int b;
};
void test()
{
Person p1;
p1.func1();
const Person p2;
//p2.func2();
}
int main()
{
test();
return 0;
}
黑马p118友元friend全局函数
/*黑马p118友元friend全局函数*/
#include
#include
using namespace std;
class Building
{
friend void goodguy(Building* b);
public:
Building()
{
this->setroom = "客厅";
this->bedroom = "卧室";
}
string setroom;
private:
string bedroom;
};
void goodguy(Building* b)
{
cout << "您的好基友要访问您的" << b->setroom << endl;
cout<< "您的好基友要访问您的" << b->bedroom << endl;
}
void test()
{
Building b;
goodguy(&b);
}
int main()
{
test();
return 0;
}
黑马p119类友元
/*黑马p119类友元friend*/
#include
#include
using namespace std;
class Building;
class Goodguy
{
public:
Goodguy();
void visit();
Building* b;
};
class Building
{
friend class Goodguy;
public:
Building();
string sittingroom;
private:
string bedroom;
};
Goodguy::Goodguy()
{
b = new Building;
}
void Goodguy::visit()
{
cout << "您的好基友正在访问" << b->sittingroom << endl;
cout << "您的好基友正在访问" << b->bedroom << endl;
}
Building::Building()
{
this->sittingroom = "客厅";
this->bedroom = "卧室";
}
void test()
{
Goodguy gg;
gg.visit();
}
int main()
{
test();
return 0;
}
黑马p120成员函数友元
/*黑马p120友元friend成员函数*/
#include
#include
using namespace std;
class Building;
class Goodguy
{
public:
Goodguy();
void visit();//可访问
void visit2();//不可访问
Building* b;
};
class Building
{
friend class Goodguy;
public:
Building();
string sittingroom;
private:
string bedroom;
};
Goodguy::Goodguy()
{
b = new Building;
}
void Goodguy::visit()
{
cout << "visit您的好基友正在访问" << b->sittingroom << endl;
cout << "visit您的好基友正在访问" << b->bedroom << endl;
}
Building::Building()
{
this->sittingroom = "客厅";
this->bedroom = "卧室";
}
void Goodguy::visit2()
{
cout << "visit2您的好基友正在访问" << b->sittingroom << endl;
}
void test()
{
Goodguy gg;
gg.visit();
gg.visit2();
}
int main()
{
test();
return 0;
}