吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1666|回复: 45
收起左侧

[已解决] C\C++每日练习第二贴,今天也要加油呀!!!

  [复制链接]
25951741 发表于 2021-2-11 12:30
#include <iostream>
#include "circle.h"
using namespace std;
//class point
//{
//public:
//        void setx(int x)
//        {
//                m_x = x;
//        }
//        int getx()
//        {
//                return m_x;
//        }
//        void sety(int y)
//        {
//                m_y = y;
//        }
//        int gety()
//        {
//                return m_y;
//        }
//private:
//        int m_x;
//        int m_y;
//};

//class circle
//{
//public:
//        void setr(int r)
//        {
//                m_r = r;
//        }
//        int getr()
//        {
//                return m_r;
//        }
//        void setcenter(point center)//获取圆心
//        {
//                mc_center = center;
//        }
//        point getcenter()
//        {
//                return mc_center;
//        }
//
//private:
//        int m_r;
//        point mc_center;
//};
//全局函数
void isincircle(circle &c,point &p)
{
        //两点之间的距离 平方
        int distance=
        (c.getcenter().getx() - p.getx())* (c.getcenter().getx() - p.getx()) +
        (c.getcenter().gety() - p.gety()) * (c.getcenter().gety() - p.gety());
        int rdistance = c.getr() * c.getr();
        if (distance==rdistance)
        {
                cout << "点在圆上" << endl;
        }
        else if(distance>rdistance)
        {
                cout << "点在圆外" << endl;
        }
        else
        {
                cout << "点在圆内" << endl;
        }
}
int main()
{
        //创建圆
        circle c;
        point center;
        center.setx(10);
        center.sety(0);
        c.setcenter(center);
        //创建点
        point p;
        p.setx(10);
        p.sety(12);
        isincircle(c, p);


        system("pause");
        return 0;
}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Eaglecad 发表于 2021-2-11 14:16
“//创建圆
        circle c;
        point center;
        center.setx(10);
        center.sety(0);
        c.setcenter(center);
        //创建点
        point p;
        p.setx(10);
        p.sety(12);
        isincircle(c, p);”这个circle的半径是多少呢
 楼主| 25951741 发表于 2021-2-13 09:40
#include <iostream>
using namespace std;
class person
{
public:
        /*构造函数,没有返回值 不用写VOID
        函数名 要和类名相同
        构造函数可以有参数 可以发生重载
        创建对象的时候构造函数会自动调用 而且只调用一次*/
        person()
        {
                cout << "构造函数:" << endl;
        }
        /*/析构函数 进行清理的操作
        没有返回值 不写void 函数明和类名相同 在名称前加~
        析构函数不可以有参数 不可以发生重载
        对象在销毁前 会自动调用析构函数 而且只会调用一次*/
        ~person()
        {
                cout << "析构函数:" << endl;
        }
};
void test01()
{
        person p;
}

int main()
{
        test01();
        //person p;
        system("pause");
        return 0;
}
 楼主| 25951741 发表于 2021-2-13 09:43
Eaglecad 发表于 2021-2-11 14:16
“//创建圆
        circle c;
        point center;

你好 这个圆和点 是分别两个class 为circle 和point  circle调用了point  在注释段里 因为我 分别做了头文件这个步骤没打出来 你把代码注释去掉就可以看到了
//private:
//        int m_r;
//        point mc_center;
//};
 楼主| 25951741 发表于 2021-2-15 12:31
#include <iostream>
using namespace std;
class person
{
public:
        person()
        {
                cout << "默认构造函数" << endl;
        }
        person(int a)
        {
                age = a;
                cout << "有参数构造函数" << endl;
        }
        int age;
        //拷贝构造函数
        person(const person &p)
        {
                age = p.age+20;
                cout << "拷贝构造函数" << endl;
        }
        ~person()
        {
                cout << "构析函数" << endl;
        }
};

void test01()
{
        //括号法
        person p;//默认构造函数调用 不要打小括号
        person p2(10);//有参构造函数  
        person p3(p2);//拷贝函数的调用
        cout<<"P2的年龄为"<<p2.age << endl;
        cout << "P3的年龄为" << p3.age << endl;

        //显示法
        person p4;
        person p5 = person(10);//有参构造
        person p6 = person(p5);//拷贝构造
        //person(20); //匿名对象 执行结束后 系统回收  person(p3) 不能成立
        //隐示法
        person p7 = 10;//person p7=person(10)
}
int main()
{
        test01();
}
 楼主| 25951741 发表于 2021-2-17 10:37
#include <iostream>
using namespace std;

class person
{
public:
        person()
        {
                cout<<"person 的默认构造函数"<<endl;
        }
        person(int age) //如果写了有参构造函数 编辑器就不提供默认构造函数
        {
                m_age = age;
                cout<<"person 的有参构造函数"<<endl;
        }
        person(const person& p) //不写 拷贝构造函数编辑器会去写一个  如果我们写了拷贝构造函数 编辑器就不提供其他普通构造函数
        {
                cout << "person 的拷贝构造函数" << endl;
                m_age = p.m_age;
        }

        ~person()
        {
                cout << "person 的析构函数" << endl;
        }
        int m_age;

};
void test01()
{
        person p;
        p.m_age = 18;
        person p2(p);
        cout<<"P2的年龄为 "<<p2.m_age <<endl;
}
void test02()
{
        person p1(20);
        person p2(p1);
        cout << "P2的年龄为 " << p2.m_age << endl;
}
void dowork(person p)
{

}
void test03()
{
        person p;
        dowork(p);
}
person dowork2()
{
        person p1;
        return p1;
}
void test04()
{
        person p = dowork2();
}


int main()
{
        test04();

        return 0;
}
 楼主| 25951741 发表于 2021-2-18 15:32
#include <iostream>
using namespace std;
class person
{
public:
        person()
        {
                cout<<"person 的默认构造函数调用"<<endl;
        }
        person(int age,int height)
        {
                m_age = age;
                m_height = new int(height);
                cout << "person 的有参构造函数调用" << endl;

        }

        person(const person &p)
        {
                cout << "person 的拷贝函数调用" << endl;
                m_age = p.m_age;//编译器默认实现的就是这个代码
                m_height=new int(*p.m_height);

        }

        ~person()
        {
                if (m_height!=NULL)
                {
                        delete m_height;
                        m_height = NULL;
                }
                cout << "person 的析构函数调用" << endl;
        }
        int m_age;
        int *m_height;
};
void test01()
{
        person p(10, 160);

        person p1(p);
        cout<<p1.m_age<<endl;
        cout << *p1.m_height << endl;

}



int main()
{
        test01();
        return 0;
}
 楼主| 25951741 发表于 2021-2-19 09:22
#include <iostream>
using namespace std;
class person
{
public:
        person(int a,int b,int c)
        {
                m_a = a;
                m_b = b;
                m_c = c;
        }

        int m_a;
        int m_b;
        int m_c;

};
class person2
{
public:
        person2() :a_a(10), a_b(20), a_c(30)
        {

        }
        int a_a;
        int a_b;
        int a_c;

};
class person3
{
public:
        person3(int a,int c,int b) :b_a(a), b_b(b), b_c(c)
        {

        }
        int b_a;
        int b_b;
        int b_c;

};
void test01()
{
        person p1(10,20,30);
        cout <<p1.m_a << endl;
}
void test02()
{
        person2 p;
        cout << p.a_b << endl;
}
void test03()
{
        person3 p2(11,12,13);
        cout << p2.b_a << endl;
}


int main()
{
        test01();
        test02();
        test03();
        return 0;
}
 楼主| 25951741 发表于 2021-2-19 10:31
#include <iostream>
using namespace std;
class phone
{
public:
        phone(string pname)
        {
                cout << "phone 的构造函数调用" << endl;
                m_pname = pname;
        }
        string m_pname;
};
class person
{
public:
        person(string name,string pname):m_name(name),m_phone(pname)
        {
                cout<<"person 的构造函数调用"<<endl;
        }
        string m_name;
        phone m_phone;
};
void test01()
{
        person p("张三","苹果");
        cout << p.m_name <<"拿着" <<p.m_phone.m_pname << endl;
}
int main()
{
        test01();
        return 0;
}
 楼主| 25951741 发表于 2021-2-20 11:16
// 静态成员函数 所有对象共享同一个函数 静态成员函数只能访问静态成员变量
#include <iostream>
using namespace std;
class person
{
public:
        static void func()
        {
                m_a = 100;//静态成员函数可以访问静态的成员变量
                //m_c = 101; 静态成员函数不可以访问非静态的成员变量 无法区分到底是那个对象的 m_c属性
                cout<<"func1静态函数调用"<<endl;
        }
        static int m_a; //静态变量是属于共享的
        static float m_b;//静态变量必须在类内声明 在类外初始化。
        double m_c;
        //静态成员函数也是有访问权限的
private:
        static void func2() //类外访问不到私有成员静态函数
        {
                cout << "func2静态函数调用" << endl;
        }
};
int person::m_a = 0;

void test()
{
        //通过对象访问
        person p;
        p.func();
        //通过类名访问
        person::func();
}

int main()
{
        test();
        return 0;
}
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-16 02:05

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表