Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Objects in C
Object Models
CS 11 - Introduction to Computer Programming I
1 Objects
2 Objects in C
Attributes
Capabilities
Relationships
Topics
1 Objects
2 Objects in C
Attributes
Capabilities
Relationships
What is a man?
http://www.gamedynamo.com/images/galleries/photo/1359/top-10-
hammy-video-games-dracula-castlevania-symphony-of-the-night.jpg
JMC Yap CS 11 - Object Models
Objects
Objects in C
What is a man?
http://www.gamedynamo.com/images/galleries/photo/1359/top-10-
hammy-video-games-dracula-castlevania-symphony-of-the-night.jpg
JMC Yap CS 11 - Object Models
Objects
Objects in C
What is a man?
http://memegenerator.net/instance/45106105
JMC Yap CS 11 - Object Models
Objects
Objects in C
What is a man?
http://knowyourmeme.com/memes/im-the-goddamn-batman
JMC Yap CS 11 - Object Models
Objects
Objects in C
Object models
Object models
Object models
Object models
Object models
Object models
Object models
Object models
Object models
A person as an object
Attributes
Name
Age
Height
Weight
A person as an object
Attributes
Name
Age
Height
Weight
Capabilities
Speak
Eat
Sleep
A person as an object
Attributes
Name
Age
Height
Weight
Capabilities
Speak
Eat
Sleep
Relationships
Mother
Father
BFF
Topics
1 Objects
2 Objects in C
Attributes
Capabilities
Relationships
Topics
1 Objects
2 Objects in C
Attributes
Capabilities
Relationships
Person isko;
Person iska;
Person people[26];
Person *person_pointer;
Initializing a struct
Initializing a struct
Topics
1 Objects
2 Objects in C
Attributes
Capabilities
Relationships
main() {
Person isko = {"Jan Michael Yap", 2000, 69.5, 100.1};
speak(isko);
eat("Adobo");
sleep(8.0);
}
Topics
1 Objects
2 Objects in C
Attributes
Capabilities
Relationships
Heterogenous relationships
Heterogenous relationships
Heterogenous relationships
Initialization
Animal p = {"dog", "Bantay"};
Person a = {"Juan", 10, 150, 50, p};
Initialization
Animal p = {"dog", "Bantay"};
Person a = {"Juan", 10, 150, 50, p};
Assignment
Animal p = {"dog"};
Person a = {"Juan", 10, 150};
a.pet = p;
strcpy(a.pet.name, "Tagpi");
Homogenous relationships
Homogenous relationships
Homogenous relationships
Homogenous relationships
main() {
Person mom = {"Martha", 40};
Person dad = {"Thomas", 45};
Person bff = {"Alfred", 80};
Person son = {"Bruce", 10};
son.mother = &mom;
son.father = &father;
son.bff = &bff;
Some Notes
For homogenous relationships, only struct pointers can be
defined inside structs.
Some Notes
For homogenous relationships, only struct pointers can be
defined inside structs.
Passing struct-typed variables to functions is much more
efficient using pass-by-reference.
Remember to use the − > operator when accessing struct
variables when doing so.
Some Notes
For homogenous relationships, only struct pointers can be
defined inside structs.
Passing struct-typed variables to functions is much more
efficient using pass-by-reference.
Remember to use the − > operator when accessing struct
variables when doing so.
For struct pointers, the . operator can be used if you use the
dereferencing (*) operator on the struct pointer.
The syntax though is different:
*(<struct pointer name>).<struct variable name>
Person a = {"Juan"};
Person *b = &a;
END OF LESSON 7