Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Agosto 2016
Algoritmos y Estructuras de Datos
1 string myName;
2 int myBirthYear;
3 int myBirthMonth;
4 int myBirthDay;
5 int myHeight;
6 int myWeight;
1 struct Student
2 {
3 short id;
4 int age;
5 double score;
6 };
Algoritmos y Estructuras de Datos
1 Student joe;
2 Student frank;
1 joe.id = 10;
2 joe.age = 20;
3 joe.score = 9.5;
Algoritmos y Estructuras de Datos
1 struct Employee
2 {
3 short id;
4 int age;
5 double wage;
6 };
7
1 struct Rectangle
2 {
3 double length = 1.0;
4 double width = 1.0;
5 };
Estructuras y funciones
1 s t r u c t Employee {
2 short i d ;
3 i n t age ;
4 double wage ;
5 };
6
7 s t r u c t Company {
8 Employee CEO; / / Employee i s a s t r u c t w i t h i n th e Company
struct
9 i n t numberOfEmployees ;
10 };
11
12 Company myCompany1 ;
13 Company myCompany2 = { { 1 , 42 , 60000.0 f } , 5 } ;
Algoritmos y Estructuras de Datos
1 c l a s s DateClass {
2 public :
3 i n t m_year ;
4 i n t m_month ;
5 i n t m_day ;
6
7 void p r i n t ( )
8 {
9 s t d : : c o u t << m_year << "/" << m_month << "/" << m_day ;
10 }
11 };
Algoritmos y Estructuras de Datos
Al igual que una estructura, una clase solo sirve para definir un
tipo de dato.
1 i n t main ( ) {
2 DateClass today { 2020 , 10 , 14 } ;
3
4 / / use member s e l e c t i o n o p e r a t o r t o s e l e c t a member v a r i a b l e
o f th e c l a s s
5 today . m_day = 1 6 ;
6 / / use member s e l e c t i o n o p e r a t o r t o c a l l a member f u n c t i o n
o f th e c l a s s
7 today . p r i n t ( ) ;
8
9 return 0;
10 }
Algoritmos y Estructuras de Datos
1 c l a s s DateClass {
2 p u b l i c : / / note use o f p u b l i c keyword here , and th e c o l o n
3 / / p u b l i c , can be accessed by anyone
4 i n t m_month ;
5 i n t m_day ;
6 i n t m_year ;
7 };
8
9 i n t main ( ) {
10 DateClass date ;
11 date . m_month = 1 0 ; / / okay because m_month i s p u b l i c
12 date . m_day = 1 4 ; / / okay because m_day i s p u b l i c
13 date . m_year = 2020; / / okay because m_year i s p u b l i c
14 return 0;
15 }
Algoritmos y Estructuras de Datos
1 c l a s s DateClass {
2 / / p r i v a t e by d e f a u l t , can o n l y be accessed by o t h e r members
3 i n t m_month ;
4 i n t m_day ;
5 i n t m_year ;
6 public :
7 / / p u b l i c , can be accessed by anyone
8 void setDate ( i n t month , i n t day , i n t year ) {
9 / / setDate ( ) can access th e p r i v a t e members o f th e c l a s s
10 / / because i t i s a member o f th e c l a s s i t s e l f
11 m_month = month ;
12 m_day = day ;
13 m_year = year ;
14 }
15 void p r i n t ( ) {
16 s t d : : c o u t << m_month << "/" << m_day << "/" << m_year ;
17 }
18 };
Algoritmos y Estructuras de Datos
1 class DateClass {
2 int m_month ;
3 int m_day ;
4 int m_year ;
5
6 public :
7 / / Note th e a d d i t i o n o f t h i s f u n c t i o n
8 void copyFrom ( const DateClass &d ) {
9 / / Note t h a t we can access th e p r i v a t e members o f d d i r e c t l y
10 m_month = d . m_month ;
11 m_day = d . m_day ;
12 m_year = d . m_year ;
13 }
14 };