Sei sulla pagina 1di 49

Objects

Objects in C

Object Models
CS 11 - Introduction to Computer Programming I

Jan Michael C. Yap


janmichaelyap@gmail.com
Department of Computer Science
University of the Philippines Diliman

JMC Yap CS 11 - Object Models


Objects
Objects in C

1 Objects

2 Objects in C
Attributes
Capabilities
Relationships

JMC Yap CS 11 - Object Models


Objects
Objects in C

Topics

1 Objects

2 Objects in C
Attributes
Capabilities
Relationships

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://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

JMC Yap CS 11 - Object Models


Objects
Objects in C

Object models

A man (or a person) has, at the very least, a name, age,


height, and weight.

JMC Yap CS 11 - Object Models


Objects
Objects in C

Object models

A man (or a person) has, at the very least, a name, age,


height, and weight.
A man can speak, eat, sleep

JMC Yap CS 11 - Object Models


Objects
Objects in C

Object models

A man (or a person) has, at the very least, a name, age,


height, and weight.
A man can speak, eat, sleep
We have “objectified” a man

JMC Yap CS 11 - Object Models


Objects
Objects in C

Object models

JMC Yap CS 11 - Object Models


Objects
Objects in C

Object models

An object (model) is an abstraction of a real world entity.

JMC Yap CS 11 - Object Models


Objects
Objects in C

Object models

An object (model) is an abstraction of a real world entity.


An object has attributes and capabilities

JMC Yap CS 11 - Object Models


Objects
Objects in C

Object models

An object (model) is an abstraction of a real world entity.


An object has attributes and capabilities
Attributes are qualities or properties that an object has.
Capabilities are actions an object can do.

JMC Yap CS 11 - Object Models


Objects
Objects in C

Object models

An object (model) is an abstraction of a real world entity.


An object has attributes and capabilities
Attributes are qualities or properties that an object has.
Capabilities are actions an object can do.
Additionally, objects can also have relationships with other
objects.

JMC Yap CS 11 - Object Models


Objects
Objects in C

A person as an object

Attributes
Name
Age
Height
Weight

JMC Yap CS 11 - Object Models


Objects
Objects in C

A person as an object

Attributes
Name
Age
Height
Weight
Capabilities
Speak
Eat
Sleep

JMC Yap CS 11 - Object Models


Objects
Objects in C

A person as an object

Attributes
Name
Age
Height
Weight
Capabilities
Speak
Eat
Sleep
Relationships
Mother
Father
BFF

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

Topics

1 Objects

2 Objects in C
Attributes
Capabilities
Relationships

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

Topics

1 Objects

2 Objects in C
Attributes
Capabilities
Relationships

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

The struct data type

struct <struct name> {


<variable declarations>
};

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

The struct data type

struct <struct name> {


<variable declarations>
};

<struct name> follows the variable naming convention, but


this time, the first letter is capitalized.

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

The struct data type

struct <struct name> {


<variable declarations>
};

<struct name> follows the variable naming convention, but


this time, the first letter is capitalized.
<variable declarations> correspond to object attributes

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

The struct data type

struct <struct name> {


<variable declarations>
};

<struct name> follows the variable naming convention, but


this time, the first letter is capitalized.
<variable declarations> correspond to object attributes
struct Person {
char name[100];
int age_in_years
float height_in_cm;
float weight_in_kg;
};

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

struct and typedef

typedef struct Person {


char name[100];
int age_in_years
float height_in_cm;
float weight_in_kg;
} Person;

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

struct and typedef

typedef struct Person {


char name[100];
int age_in_years
float height_in_cm;
float weight_in_kg;
} Person;

/* This has same effect as the one above. */


typedef struct {
char name[100];
int age_in_years
float height_in_cm;
float weight_in_kg;
} Person;

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

struct and typedef

typedef struct Person {


char name[100];
int age_in_years
float height_in_cm;
float weight_in_kg;
} Person;

/* This has same effect as the one above. */


typedef struct {
char name[100];
int age_in_years
float height_in_cm;
float weight_in_kg;
} Person;

Person isko;
Person iska;
Person people[26];
Person *person_pointer;

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

Initializing a struct

Person isko = {"Jan Michael Yap", 2000, 175.26, 100.1};

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

Initializing a struct

Person isko = {"Jan Michael Yap", 2000, 175.26, 100.1};

Person isko_clone = isko;

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

Assigning struct variables

Person isko = {"Jan Michael Yap", 2000, 69.5, 100.1};

/* We use the . operator to access struct variables */


isko.age = 3000;
isko.weight_in_kg = 50.5;
isko.height_in_cm = 200.0;
strcpy(isko.name, "The Goddamn Batman");

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

Topics

1 Objects

2 Objects in C
Attributes
Capabilities
Relationships

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

“Assigning” capabilities to the Person object

void speak(Person tao) {


printf("Hi. I am %s. I am %d years old. I am %f cm high. I weigh %f kg.\n",
tao.name, tao.age, tao.height_in_cm, tao.weight_in_cm);
}

void eat(char[] food) {


printf("I am going to eat %s.\n", food)
}

void sleep(float hours_to_sleep) {


printf("I will sleep for %f hours", hours_to_sleep);
}

main() {
Person isko = {"Jan Michael Yap", 2000, 69.5, 100.1};
speak(isko);
eat("Adobo");
sleep(8.0);
}

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

Topics

1 Objects

2 Objects in C
Attributes
Capabilities
Relationships

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

Heterogenous relationships

By heterogenous, it is meant that the relationship is between


two or more different objects.

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

Heterogenous relationships

By heterogenous, it is meant that the relationship is between


two or more different objects.
Example, add a pet variable to Person object. pet is to be of
type Animal.

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

Heterogenous relationships

By heterogenous, it is meant that the relationship is between


two or more different objects.
Example, add a pet variable to Person object. pet is to be of
type Animal.
typedef struct Animal{
char species[100];
char name[100]
} Animal;

typedef struct Person {


char name[100];
int age_in_years
float height_in_cm;
float weight_in_kg;
Animal pet;
} Person;

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

Heterogenous relationships: initialization and assignment

Initialization
Animal p = {"dog", "Bantay"};
Person a = {"Juan", 10, 150, 50, p};

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

Heterogenous relationships: initialization and assignment

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");

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

Homogenous relationships

By homogenous, it is meant that the relationship is between


two or more of the same objects.

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

Homogenous relationships

By homogenous, it is meant that the relationship is between


two or more of the same objects.
Example, in the Person object, add mother, father, and bff
variables all of type Person.

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

Homogenous relationships

By homogenous, it is meant that the relationship is between


two or more of the same objects.
Example, in the Person object, add mother, father, and bff
variables all of type Person.
Instinctively,
typedef struct Person {
char name[100];
int age_in_years
float height_in_cm;
float weight_in_kg;
Person mother;
Person father;
Person bff;
} Person;

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

Homogenous relationships

By homogenous, it is meant that the relationship is between


two or more of the same objects.
Example, in the Person object, add mother, father, and bff
variables all of type Person.
Instinctively,
typedef struct Person {
char name[100];
int age_in_years
float height_in_cm;
float weight_in_kg;
Person mother;
Person father;
Person bff;
} Person;

WILL NOT EVEN COMPILE!!!

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

Homogenous relationships: struct pointers within a struct

Declare pointers to struct types instead.


typedef struct Person {
char name[100];
int age_in_years
float height_in_cm;
float weight_in_kg;

/* The pointers have to be of a specific struct‘‘type",


i.e. the struct name */
struct Person *mother;
struct Person *father;
struct Person *bff;
} Person;

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

Homogenous relationships: accessing struct pointer


variables

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;

/* To access pointer variables within a struct data typed variable,


the -> operator is used. */
printf("Hi, my name is %s. My mother is %s. My father is %s. My BFF is %s\n",
son.mother->name, son.father->name, son.bff->name);
}

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

Some Notes
For homogenous relationships, only struct pointers can be
defined inside structs.

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

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.

JMC Yap CS 11 - Object Models


Attributes
Objects
Capabilities
Objects in C
Relationships

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;

/* The following two statements are equivalent */


b->name;
*(b).name;

JMC Yap CS 11 - Object Models


Objects
Objects in C

END OF LESSON 7

JMC Yap CS 11 - Object Models

Potrebbero piacerti anche