#include<iostream> using namespace std; class Student{ public: int num; char* name; float score; Student(int num,char* name,float score):num(num),name(name),score(score){} void display() { cout<<"num:"<<num<<"name:"<<name<<"score:"<<score<<endl; //private: } }; class Graduate:public Student{ public: Graduate(int num,char* name,float score,float pay):Student(num, name, score), pay(pay){} //特别注意 void display() { cout<<"num:"<<num<<"name:"<<name<<"score:"<<score<<"score:"<<score<<endl; } private: float pay; }; int main() { Student stud1(1001,"Li",87.5); Graduate grad1(2001,"Wang",98.5,563.5); Student *pt=&stud1; pt->display(); grad1.display(); return 0; }
#include<iostream> using namespace std; #include<string.h> class employee { protected: char number[50]; char name[30]; char sexity[10]; char position[30]; double wage; class Date { private: static int year; static int month; static int day; public: Date() {} static void SetYear(int _year) { year= _year; } static void SetMonth(int _month) { month = _month; } static void SetDay(int _day) { day = _day; } static int GetYear() { return year; } static int GetMonth() { return month; } static int GetDay() { return day; } }; public: employee(char* number,char* name,char* sexity,char* position,int year,int month,int day) { strcpy(this->number,number); strcpy(this->name,name); strcpy(this->sexity,sexity); strcpy(this->position,position); Date::SetYear(year); Date::SetMonth(month); Date::SetDay(day); } employee(char* number="0",char* name="jiangwei",char* sexity="male",char* position="employee",int year=1994,int month=7,int day=28,double wage=0) { //number = new char[x]; ; strcpy(this->number,number); strcpy(this->name,name); strcpy(this->sexity,sexity); strcpy(this->position,position); Date::SetYear(year); Date::SetMonth(month); Date::SetDay(day); } }; int employee::Date::year = 0; int employee::Date::month = 0; int employee::Date::day = 0; class technician : public employee { protected: double worktime; public: technician(char* number,char* name,char* sexity,char* position,double worktime,int year,int month,int day) { this->worktime=worktime; } void display() { wage=25*worktime; cout<<"wage:"<<wage<<endl; } }; void main() { technician p1("123456","weng","male","director",8,1994,7,28); p1.display(); }
#include<stdio.h> #include<string.h> int main() { int t3[4]={'0',0,'\0'}; printf("%d\n",sizeof(t3));//16 printf("%d\n",strlen((char*)t3));//1 }
相关推荐
虽然面向对象的设计并不会在很大程度上依赖于某种语言,但现代著作中提及面向对象的实现一般都认为是C++, Smalltalk, 或者Java。 本文从较底层的视角用面向过程的语言(比如C)对面向对象予以实现,这对于一些想运用...
C语言实现对象编程之继承代码,总共包含两种方式的代码,代码非常详细,可以在Linux平台和Windows平台编译执行。
用C语言实现继承的研究.pdf
使用C语言实现封装,继承,多态等面向对象的特性使用C语言实现封装,继承,多态等面向对象的特性使用C语言实现封装,继承,多态等面向对象的特性使用C语言实现封装,继承,多态等面向对象的特性使用C语言实现封装,...
C语言实现类的封装、继承和多态,基类、派生类、结构体实现类
C++是C语言的继承,它既可以进行C语言的过程化程序设计,又可以进行以抽象数据类型为特点的基于对象的程序设计,还可以进行以继承和多态为特点的面向对象的程序设计。C++擅长面向对象程序设计的同时,还可以进行基于...
这里说的继承有点像C++里的父类和子类,实际上是结构体类型的强制转换,最近看Linux内核源码时经常接触到这种方法,在这里就当作是一个简单的学习吧。
C语言程序设计 继承性和派生类PPT课件.pptx
C语言程序设计 继承性和派生类PPT学习教案.pptx
难道在C语言中一个结构不能包含指向自己的指针吗? o 2.7 怎样建立和理解非常复杂的声明?例如定义一个包含 N 个指向返回指向字符的指针的函数的指针的数组? o 2.8 函数只定义了一次, 调用了一次, 但编译器提示...
C语言:多态(单继承实现)源码
详见blog.csdn.net/wormsun
*2.5 在C语言中是否有模拟继承等面向对象程序设计特性的好方法? 22 2.6 为什么声明extern f(struct x *p); 给我报了一个晦涩难懂的警告信息? 23 2.7 我遇到这样声明结构的代码:struct name {int namelen; ...
用C语言模拟了 c++ 封装、继承、多态三大特性。
C语言程序设计继承与派生PPT课件.pptx
*2.5 在C语言中是否有模拟继承等面向对象程序设计特性的好方法? 2.6 为什么声明externf(structx*p);给我报了一个晦涩难懂的警告信息? 2.7 我遇到这样声明结构的代码:structname{intnamelen;charnamestr[1];}...
对于嵌入式Linux开发类职位来说,最重要的是C语言基本功底,对C语言要了解的十分透彻,比如C语言的面向对象编程,怎么用C语言去实现面向对象类高级语言的继承、多态、封装。达到熟练掌握数据结构,尤其是链表。对于...
C语言程序设计继承与派生PPT学习教案.pptx
目 录 序言 前言 第1章 程序设计与算法 1 1.1 程序设计语言的发展 1 ...10.5 派生类与继承类 225 10.5.1 单继承的派生类 225 10.5.2 多继承的派生类 233 附录A 常用字符与ASCII代码对照表 238 附录B 习题 239