首页 > 其他分享 >每日打卡

每日打卡

时间:2023-04-18 22:15:11浏览次数:30  
标签:string int void float public 打卡 每日 cout

虚函数,纯虚函数不能直接定义对象,可以定义指针,但他的派生可以定义对象;

注意最后一道题,纯虚数,派生的类仍为纯虚数,因为派生中没有将基类的全部纯虚数重新定义;

a=d;

*a=&d;

&a=d;

#include<iostream>
using namespace std;
class people
{
protected:
int age;
string name;
public:
people() {};
people(int x,string y):age(x),name(y){}
~people() {};
void setValue(int m, string str)
{
age = m; name = str;
}
virtual void display()const = 0;
};
class student:public people
{
private:
int studentID;
public:
student(){}
student(int n):studentID(n){}
~student(){}
void setID(int m)
{
studentID = m;
}
void display()const
{
cout << name << " " << age << " " << studentID << endl;
}
};
class teacher :public people
{
private:
int teacherID;
public:
teacher(){}
teacher(int n):teacherID(n){}
~teacher(){}
void setID(int m)
{
teacherID = m;
}
void display()const
{
cout << name << " " << age << " " << teacherID << endl;
}
};
int main()
{
student a;
teacher b;
people *c;
c = &a;
c->setValue(10, "zhao");
a.setID(123);
c->display();
c = &b;
c->setValue(20, "liu");
b.setID(456);
c->display();
return 0;
}
#include<iostream>
using namespace std;
class Animal
{
public:
Animal(){}
virtual void speak()
{
cout << "My name is Animal."<<endl;
}
};
class Cat :public Animal
{
public:
Cat(){}
void speak()
{
cout << "My name si Cat."<<endl;
}
};
class Leopard :public Animal
{
public:
Leopard(){}
void speak()
{
cout << "My name is Leopard." << endl;
}
};
void fun(Animal *p)
{
p->speak();
}
int main()
{
Animal a;
Cat b;
Leopard c;
//a = &b;
//a->speak();
//a = &c;
//a->speak();
fun(&a);
fun(&b);
fun(&c);
return 0;
}
#include<iostream>
#define PI 3.1415
using namespace std;
class shape
{
public:
virtual void setvalues() = 0;
virtual float area() = 0;
};
class rectangle :public shape
{
public:
float x, y;
public:
void setvalues()
{
float n; float m;
cin >> n >> m;
x = n;
y = m;
}
float area()
{

return x * y;
}
};
class sanjiaoxing:public shape
{
public:
float x, y;
public:
void setvalues()
{
float n, m;
cin >> n >> m;
x = n;
y = m;
}
float area()
{
return (float)(x * y)/2;
}

};
class zhengfangxing :public shape
{
public:
float x;
public:
void setvalues()
{
float n;
cin >> n;
x = n;

}
float area()
{
return x * x;
}

};
class yuanxing :public shape
{
public:
float x;
public:
void setvalues()
{
float n;
cin >> n;
x = n;

}
float area()
{
return x * x*PI;
}

};
int main()
{
shape* a;
rectangle b;
sanjiaoxing c;
zhengfangxing d;
yuanxing e;
a = &b;
a->setvalues();
if (b.x <= 0 || b.y <= 0)
{
cout << "Set Value Error!";
return 0;
}
cout<<a->area()<<endl;
a = &c;
a->setvalues();
if (c.x <= 0 || c.y <= 0)
{
cout << "Set Value Error!";
return 0;
}
cout << a->area()<<endl;
a = &d;
a->setvalues();
if (d.x <= 0)
{
cout << "Set Value Error!";
return 0;
}
cout << a->area()<<endl;
a = &e;
a->setvalues();
if (e.x <= 0)
{
cout << "Set Value Error!";
return 0;
}
cout << a->area();
}
#include<iostream>
using namespace std;
class student
{
protected:
string name;
int studentID;
public:
student(string x, int y) :name(x), studentID(y){}
void setnameid()
{
int x;
string y;
cin >>y >>x;
name = y;
studentID = x;
}
void displaynameid()
{
cout << name <<studentID<<endl;
}
virtual void setmajor() = 0;
virtual void getmajor() = 0;
virtual void setadvisor() = 0;
virtual string getadvisor() = 0;

};
class understud :public student//也是抽象类
{
protected:
string major;
public:
understud(string x, int y, string z) :student(x, y)
{
major = z;
}
void setmajor()
{
string x;
cin >> x;
major = x;
}
void getmajor()
{
cout << major<<endl;
}
};
class poststud :public understud
{
protected:
string advisor;
public:
poststud(string x="***", int y=-1, string z="###", string m ="&&&") :understud(x,y,z)
{
advisor = m;
}
void setadvisor()
{
string x;
cin >> x;
advisor = x;
}
string getadvisor()
{
return advisor;
}
};
int main()
{
student* a;
understud *b;
poststud d;
int q;
cin >> q;
if (q ==1)
{
a = &d;
a->setnameid();
a->setmajor();
a->setadvisor();
a->displaynameid();
a->getmajor();
cout << a->getadvisor() << endl;
}
if (q == 0)
{
d;
a = &d;
a->displaynameid();
a->getmajor();
cout << a->getadvisor() << endl;
}
return 0;
}

标签:string,int,void,float,public,打卡,每日,cout
From: https://www.cnblogs.com/zhaoqianwan/p/17331365.html

相关文章

  • 第三天打卡
    #include<iostream>usingnamespacestd;intmain(){intyear,mouth,day,x=0,i;inta[12]={31,28,31,30,31,30,31,31,30,31,30,31};scanf("%d%d%d",&year,&mouth,&day);if(year-1990<=2)x=(year-1990)*365+day;......
  • 每日总结-23.4.18
    <%@pageimport="zhengcechaxun.Pd_zhengce"%><%@pageimport="zhengcechaxun.Thesql"%><%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%>&......
  • 每日打卡-7
    一.问题描述n个小伙伴(编号从0到n-1)围坐一圈玩游戏。按照顺时针方向给n个位置编号,从0到n-1。最初,第0号小伙伴在第0号位置,第1号小伙伴在第1号位置,……,依此类推。游戏规则如下:每一轮第0号位置上的小伙伴顺时针走到第m号位置,第1号位置小伙伴走到第m+1号位......
  • 编程打卡:C语言趣味编程习题做
    编程打卡:C语言趣味编程习题做存钱问题问题描述给定不同期限档次整存整取的月利率,期限和本金,求出使利息最大的存款方案。设计思路遍历每种可能的存钱方案,求出利息最大的方案,然后输出。流程图graphA[开始]-->B[定义各种各样的变量]-->C[遍历所有存款方案,保存利率最大的......
  • 每日打卡
    最佳存款问题:问题描述:银行一年零存整取的利率为0.63%某人手里有一笔钱,存入银行,他想每年取1000元,问他最开始存了多少钱问题分析:用逆向思路,从第五年末入手,第五年末正好取走最后1000元,所以第五年存款数为1000/(1+12*0.0063)则前四年亦是如此代码#include<stdio.h>main(){    ......
  • 打卡第三天
    一、问题描述: 从键盘上输入4个整数,输出其中的最小值二、设计思路:从键盘上输入四个整数,然后找到其中的最小值,并将其输出。具体实现的方法是,先定义四个整数变量a、b、c、d,然后使用cin语句从键盘上依次读入这四个整数。接下来,我们可以用if语句来比较这些数,并找到其中的最小值,将其赋......
  • 4.18打卡
    一、问题描述:一辆卡车违反交通规则,撞人后逃跑。现场有三人目击该事件,但都没有记住车号,只记下车号的一些特征。甲说:牌照的前两位数字是相同的;乙说:牌照的后两位数字是相同的,但与前两位不同;丙是数学家,他说:四位的车号刚好是一个整数的平方。请根据以上线索求出车号。二、设计思路:该......
  • 4/18打卡 复数的输入输出和加减乘除重载
    classComplex{doublereal;doubleimag;public:friendistream&operator>>(istream&is,Complex&c);friendostream&operator<<(ostream&os,constComplex&a);Complex(){real=0;......
  • 建民の每日打卡7
    一、问题描述银行月利息0.63%,某人在第一年存了一笔钱,每年年底取出1000,五年后正好取完,求第一年存了多少钱?二、流程设计1.从最后一年向前递推,年末加上1000,除以年利率求出去年余款2.循环四次完成递推3.输出答案三、流程图设计 四、代码实现#include<iostream>usingnamesp......
  • 天天打卡一小时——5
    一.问题描述读入一系列整数,统计出正整数和负整数的个数,读到0结束二.设计思路1.输入一组整数2.包含正整数和负整数3.在每次读完一个数后需要进行判断4.非0接着读,为0则结束程序5.使用while语句三.程序流程图 四.代码实现#include<iostream>usingnamespacestd;int......