3DProgramming first lesson data type 20210830

preface

Course objective: to further master the program development of C + + language, and on this basis, to master the object-oriented idea and the development of three-dimensional interactive program of OpenGL. Learn to develop projects based on OpenGL SDK and solve problems encountered in the actual development process through break point debugging.

Course schedule:
Main contents:

  • Custom data type
  • function
  • Classes and objects
  • Class encapsulation, inheritance, polymorphism
  • Camera class
  • Model import class (static model, dynamic model)
  • Texture map class
  • Program framework class
  • Particle effects
  • Develop 3D roaming programs (such as 3D games or 3D interactive programs)

1, Program development process?

  1. The source program is processed into the target program (result program) by means of a translator.

  2. Three different types of translation programs

    (a) Assembler: used to translate source programs written in assembly language
    (b) Compiler: used to translate source programs written in high-level programming language.
    (c) Interpreter: it is also a source program written in high-level programming language. Its difference from compiler is that it is executed while translating.

  3. The development process of the program: editing, compiling, connecting, running and debugging.

2, Basic data type

1.char,int,float,double

sizeof(char)=1
sizeof(int)=4
sizeof(float)=4
sizeof(double)=8

Common pointer types occupy four bytes.

2. Basic data type - pointer

  • What is a memory address?
    Memory address is a unique numerical code representing a memory unit containing data.

  • What is a pointer variable?
    A pointer variable is a variable that stores a computer's memory address.

  • What is the value of a pointer?
    Reading data from the memory pointed to by the pointer is called the value of the pointer.

  • Pointer type (int* float *)
    Pointers can also be void types, NULL pointers, and uninitialized pointers.
    Void literally means "no type", void * is "no type pointer", and void * can point to any type of data.
    A NULL pointer is a constant with a value of zero defined in the standard library.

The code is as follows (example):

    int *ip;
	float *fp;
	char *cp;
	char *pstr = "Hello world";
	cout << sizeof(ip) << endl;//4 pointers occupy four bytes of memory space
	cout << sizeof(fp) << endl;//4
	cout << sizeof(cp) << endl;//4
	cout << sizeof(pstr) << endl;//4
	cout << strlen(pstr) << endl;//If there is no space in the middle, it is 10. Calculate the actual length of the string


3, Custom data types (enum typedef, struct, union, class)

1. Enum type - enum

  • Declaration form: enum enumeration type name {variable value list};
  • Enumeration variables are treated as constants and cannot be assigned values. For example: Sun=0; However, you can specify the value of the enumeration element at the time of declaration, but not later, such as enum weekday{sun=7,mon=1,tue,wed,thu,fri,sat};
  • Enumeration values can be used for relational operations.
  • Integer values cannot be directly assigned to enumeration variables, but must be cast.
    However, enumeration elements can be assigned directly to integer variables.
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;

enum enum_Weekday { Sunday = 7, Monday = 1, Tuesday, Wednsday, Thursday, Friday, Saturday };//Defines the enumeration type of a week


int main()
{
	int a;
	enum enum_Weekday day;  //Defines a variable of type week enumeration
	day = (enum_Weekday)5;//Cast integer values cannot be directly assigned to enumeration variables. If an integer needs to be assigned to an enumeration variable, cast should be performed.
	//day = Saturday;

	a = Monday;  //Integer values cannot be directly assigned to enumeration variables (to force type conversion), but enumeration elements can be assigned to integer variables
	cout << day<<endl;
	cout << a << endl;

    return 0;
}

2,typedef

Four usages

  • Alias a defined variable type
  • Define function pointer type
  • Define array pointer type
  • Define array type

See for details https://blog.csdn.net/qq_31073871/article/details/81258558

3,struct

struct Stu
{
int xuehao;
char* pName;
char score[10];
};
struct Stu stu1;
struct Stu
{
int xuehao;
char* pName;
char score[10];
}stu1;
typedef struct stu
{
int xuehao;
char *pName;
char score[10];
} Stu_Struct,*pStu_Struct;
Stu_Struct stu1,stu2;
pStu_Struct pstu1;

Alignment of struct
https://blog.csdn.net/d04421024/article/details/5644945

The offset of the starting address of each variable relative to the starting address must be a multiple of the number of bytes occupied by the type of the variable( In order to improve the storage speed of CPU)
Generally speaking, the number of bytes occupied by struct will not be roughly equal to the sum of the number of bytes occupied by each member variable.

  • The memory occupied by the structure is greater than or equal to the total memory occupied by all members (there may be gaps between members)

give an example:

struct MyStruct
{
    double dda1;
	char dda;
	int type;
};
sizeof(MyStruct)=16  //Instead of roughly adding 8 + 1 + 4 = 13

4. union Community

union common name
{
Data type member name;
Data type member name;
...
}Variable name;

  • The difference between a structure and a common body is that each member of the structure occupies different memory and has no influence on each other; All members of the community occupy the same memory. Modifying one member will affect all other members.
  • The memory occupied by the structure is greater than or equal to the total memory occupied by all members (there may be gaps between members). The memory occupied by the community is equal to the memory occupied by the longest member. The common body uses the memory overwrite technology. Only one member's value can be saved at the same time. If a new member is assigned, the original value will be overwritten.

Tags: C C++

Posted on Fri, 03 Sep 2021 17:32:38 -0400 by mutedgirl