Structral Data and Abstraction
结构化数据及抽象
by 方泓睿
With reveal.js and plantUML
Data Structure using struct
A data structure is a group of data elements grouped together under one name.
These data elements,known as member,can have different types and different length. Data structure can be declared in C++ using the following syntax:
// Declare and define the type in the same place.
// And also declare the object_name
// in C/C++
struct type_name{
member_type member_name_1;
member_type member_name_2;
member_type member_name_3;
// and so on ...
} object_names;// You can also seperately declare and define the type
struct type_name;
struct type_name{
member_type member_name_1;
member_type member_name_2;
member_type member_name_3;
// and so on ...
} ;
// To declare a variable
// In C/C++
struct type_name object_names;
// In C++ you can ignore the struct keyword
type_name object_names;Where type_name is a name for structure type ,object_names can be a sets of valid identifiers for objects that have the type of this structure.Within the braces{},there is a list with data members,each one is specified with data type and a valid identifier as its name.
Note that a semicolon should be at the end of object_names if there is any or follow the curly brace if there is none object_name.
Let's see an example...
struct Product{
int weight;
double price;
};
Product apple,banana;
Product melon;This declare a structure type named Product,and define it has two members:weight and price,each of a fundamental type.This declaration creates a new type ,which is then used to declare three objects (variables) of this type: apple ,banana and melon.Note that once the Product is declare,it can be used as any other common types.
Right at the end of the struct definition,and before the ending semicolon(;),the oprional field object_names can be used to directly declare objects of the structure type. For example
struct Product{
int weight;
double price;
} apple,banana ,melon;Text
#include <iostream>
#include <string>
using std::cin,std::cout,std::endl,std::string;
struct Movie{
string title;
int year;
};
auto PrintMovie(Movie) -> void;
auto main()-> int{
Movie m;
m.title="Avengers:Endgame";
m.year=2019;
cout<<"My favourite movie is";
PrintMovie(m);
cout<<"Title of you favourite is ";
cin>>m.title;
cout<<"And year is ";
cin>>m.year;
cout<<"So your favourite movie is";
PrintMovie(m);
}
auto PrintMovie(Movie m) -> void {
cout<<m.title<<"("<<m.year<<")"<<endl;
}#include <iostream>
#include <string>
using std::cout,std::endl,std::string,std::cin;
struct Movie{
string title;
int year;
}
auto PtintMovie(Movie m)->void;
auto PrintMovie(Movie *m)->void;
auto main()->void{
Movie m;
Movie *m_ptr;
m_ptr=&m;
cin>>m_ptr->title;
cout<<"And year is ";
cin>>m_ptr->year;
cout<<"So your favourite movie is";
PrintMovie(m);
PrintMovie(m_ptr);
}
auto PrintMovie(Movie m)->void{
cout<<m.title<<"("<<m.year<<")"<<endl;
}
auto PrintMovie(Movie *m)->void{
cout<<(m->title)<<"("<<(m->year)<<")"<<endl;
}
#include <iostream>
#include <string>
using std::cout,std::endl,std::string,std::cin;
struct Movie{
string title,
int year;
};
Movie Movies[2];
auto PrintMovie()->void;
auto main()->int{
Movies[0].title="Avengers:Endgame";
Movies[0].year=2019;
cout<<"My favourite movie is";
PrintMovie(Movies[0]);
cout<<"Title of you favourite is ";
cin>>Movies[1].title;
cout<<"And year is ";
cin>>Movies[1].year;
cout<<"So your favourite movie is";
PrintMovie(Movies[1]);
}
auto PrintMovie(Movie m) -> void {
cout<<m.title<<"("<<m.year<<")"<<endl;
}2019 Chaoyue Summer Camp Presentation 3
By 方泓睿
2019 Chaoyue Summer Camp Presentation 3
- 26