目录
黑马p99学生类
/*黑马p99学生类,属性姓名和学号,赋值,显示*/
#include<iostream>
#include<string>
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;
}
黑马p103类封装属性私有
/*黑马p103类封装属性私有,姓名可读可写,年龄可读不可写,情人可写不可读*/
#include<iostream>
#include<string>
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:"<<p.showname()<<endl;
cout<<"age:"<<p.showage()<<endl;
p.getlover("1");
return 0;
}
黑马p104立方体类全局成员函数面积体积
/*立方体类,求体积面积,全局、成员函数证同*/
#include<iostream>
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点类圆类关系*/
#include<iostream>
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<iostream>
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<<endl;
cout << "p3的年龄是:" << p3.page << endl;
cout << "显示调用:" << endl;
Person p4 = Person();
Person p5 = Person(11);
Person p6 = Person(p5);
cout << "p5的年龄是:" << p5.page << endl;
cout << "p6的年龄是:" << p6.page << endl;
cout << "隐性调用:" << endl;
Person p7;
Person p8 = 12;
Person p9 = p8;
cout << "p8的年龄是:" << p8.page << endl;
cout << "p9的年龄是:" << p9.page << endl;
}
int main()
{
test();
return 0;
}
黑马p111列表初始化
/*类里有开辟堆区(指针),拷贝构造函数要重新写,重新申请*/
/*黑马p107拷贝构造函数*/
#include<iostream>
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<<endl;
cout << "p3的年龄是:" << p3.page << " 身高是:" << *p1.pheight << endl;
}
int main()
{
test();
return 0;
}
黑马p111列表初始化
/*类里有开辟堆区(指针),拷贝构造函数要重新写,重新申请*/
/*黑马p107拷贝构造函数*/
#include<iostream>
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<iostream>
#include<string>
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<iostream>
#include<string>
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<<Person::a;
}
int main()
{
test();
return 0;
}
黑马p115this
/*黑马p115this*/
#include<iostream>
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<iostream>
using namespace std;
class Person
{
public:
void showclassname()
{
cout << "这还是Person类"<<endl;
}
void showage()
{
if (this == NULL)
return;
cout << "age:" << age << endl;
}
int age;
};
void test()
{
Person* p = NULL;
p->showclassname();
p->showage();
}
int main()
{
test();
return 0;
}
黑马p117const类
/*黑马p117const类*/
#include<iostream>
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<iostream>
#include<string>
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<iostream>
#include<string>
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<iostream>
#include<string>
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;
}