// cool.h 头文件,定义父类和子类。
typedef struct {
char* name;
int age;
} Person;
void setName(Person* person, char* name);
char* getName(Person* person);
void setAge(Person* person, int age);
int getAge(Person* person);
typedef struct {
Person person;
char** listOfCourses;
int numOfCourses;
} Student;
void setListOfCourses(Student* student, char* listOfCourses[], int numOfCourses);
void printListOfCourses(Student* student);
// person.c 源文件,父类行为原型的实现。
#include <stdlib.h>
#include <string.h>
#include "cool.h"
void setName(Person* person, char* name) {
person->name = malloc(sizeof(strlen(name) + 1));
strcpy(person->name, name);
}
char* getName(Person* person) {
return person->name;
}
void setAge(Person* person, int age) {
person->age = age;
}
int getAge(Person* person) {
return person->age;
}
// student.c源文件,子类行为原型的实现。
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "cool.h"
void setListOfCourses(Student* student, char* listOfCourses[], int numOfCourses) {
int i;
char** temp;
student->numOfCourses = numOfCourses;
temp = malloc(numOfCourses * sizeof(char*));
student->listOfCourses = temp;
for (i = 0; i < numOfCourses; i++) {
*temp = malloc(sizeof(strlen(*listOfCourses) + 1));
strcpy(*temp, *listOfCourses);
temp++;
listOfCourses++;
}
}
void printListOfCourses(Student* student) {
int i;
char** temp;
temp = student->listOfCourses;
printf("%s' major is ", ((Person*)student)->name);
for (i = 0; i < student->numOfCourses-1; i++) {
printf("%s, ", *temp++);
}
printf("and %s.\n", *temp);
}
// main.c 主源文件,观察多态现象。
#include <string.h>
#include <stdio.h>
#include "cool.h"
int main() {
Person* lily;
lily = malloc(sizeof(Person));
setName(lily, "Lily");
setAge(lily, 19);
printf("%s is %d years old.\n", getName(lily), getAge(lily));
Student* peter;
peter = malloc(sizeof(Student));
setName((Person*) peter, "Peter");
setAge((Person*) peter, 20);
printf("%s is %d years old.\n", getName((Person*) peter), getAge((Person*) peter));
char* listOfCourses[] = { "poem", "music", "movie" };
int numOfCourses = sizeof(listOfCourses) / sizeof(listOfCourses[0]);
setListOfCourses(peter, listOfCourses, numOfCourses);
printListOfCourses(peter);
return 0;
}
分享到:
相关推荐
虽然C++领域的经典书籍犹如过江之鲫、车载斗量,但...《Thinking in C++》卷1和《Thinking in Java》最大的特点就是结合面向对象的设计思想来讲解C++、Java程序设计,在阐述OOP方面做得非常好!其他C++书籍无出其右。
这是一本17年前的书,写的是用纯C来实现OOP。。。 亘古的C,永远的C。。。 关天C,我没有其它要说的了。 关于这个资源,还有一句:原书+源码,全在这儿 我希望10年后我还在读它,看它。。。 No programming ...
What is OOP or object oriented programming and how it impacts Objective-C What are the Objective-C language features and the foundational APIs How to use the Filesystem, I/O and networking APIs How to...
flow statements. Although references will often be made to C and C++ language features, these are not intended to be insider comments, but instead to help all programmers put C# in ...
该资料库充斥着理论幻灯片,程序数量,概念清晰的项目,并经过了精美的解释,有据可查的学习内容,可以在大学期间表达我在OOP中的学习内容,并帮助您掌握相同的知识!目录没有。 章名链接查看1。 简洁的C语言回顾...
Paul Deitel and Harvey Deitel are the founders of Deitel & Associates, Inc., the internationally recognized programming languages authoring and corporate-training organization. Millions of people ...
In the 70s, Brian Kernighan and Dennis Ritchie created the language C. It was quite similar to human language, making it easier and faster to write code, while keeping a high level of abstraction. ...
of the C# language as a whole, the pillars of object oriented programming (OOP), assembly configuration, database access (through ADO.NET), as well as the process of building desktop GUI applications,...
Some previous experience with OOP programming in other languages and a good knowledge of JavaScript are assumed. In Detail The Dart programming language can be used for both client- and server-side ...
Swift is a multi-paradigm programming language enabling you to tackle different problems in various ways. Understanding each paradigm and knowing when and how to utilize and combine them can lead to a...
of the C# language as a whole, the pillars of object oriented programming (OOP), assembly configuration, database access (through ADO.NET), as well as the process of building desktop GUI applications,...
Using Traits And Oop In Rust Chapter 7. Ensuring Memory Safety And Pointers Chapter 8. Organizing Code And Macros Chapter 9. Concurrency - Coding For Multicore Execution Chapter 10. Programming At ...
The book covers the SystemVerilog verification constructs such as classes, program blocks, C interface, randomization, and functional coverage. SystemVerilog for Verification also reviews some design...
The book covers the SystemVerilog verification constructs such as classes, program blocks, C interface, randomization, and functional coverage. SystemVerilog for Verification also reviews some design...
Chapter 14: Extensions in C/C++, System Calls, and C/C++ Libraries Chapter 15: Packaging – Creating Your Own Libraries or Applications Title: Python: Journey from Novice to Expert Author: Dusty ...
Gain a deep understanding of Python’s objects and memory model as well as its OOP features, including those found in Python’s new-style classes Build more effective Web, CGI, Internet, and network ...
Model real-world objects in Swift and have an in-depth understanding of the data structures used, along with OOP concepts and protocols Use CocoaPods, an open source Swift package manager to ease your...
Model real-world objects in Swift and have an in-depth understanding of the data structures used, along with OOP concepts and protocols Use CocoaPods, an open source Swift package manager to ease your...
Gain a deep understanding of Python’s objects and memory model as well as its OOP features, including those found in Python’s new-style classes Build more effective Web, CGI, Internet, and network ...