当前位置: 首页 > news >正文

C++语法学习的主要内容

 

科技特长生方向,主要学习的内容为 

一,《C++语法》

二,《数据结构》

三,《算法》

四,《计算机基础知识》

五,《初高中的数学知识》

其中,《C++语法》学习的主要内容如下:

1,cout输出语句和键盘的基本操作

#include <iostream>
int main()
{
    std::cout << "dsaffd\n";
}

2,int整数变量的定义和加减运算

#include <iostream>
int main()
{
    int a;
    a = 10;
    int b = 20;
    int c;
    c = a + b;
    std::cout << "c=" << c << std::endl;
}

3,cin输入和if else 判断语句的使用

#include <iostream>
int main()
{
    int a, b;
    std::cin >> a >> b;
    if (a > b)
    {
        std::cout << "a大" << std::endl;
    }
    else 
    {
        std::cout << "b大" << std::endl;
    }
}

4,float变量的定义和使用

#include <iostream>
using namespace std;
int main()
{
    cout << "计算器程序 加法输入1 减法输入2"<<endl;
    int a;
    cin >> a;
    if (a == 1)
    {
        float b, c, d;
        cout << "请输入第1个数 然后按回车" << endl;
        cin >> b;
        cout << "请输入第2个数 然后按回车" << endl;
        cin >> c;
        d = b + c;
        cout << "两个数的和是" << d << endl;
    }    
    else if (a == 2)
    {
        float b, c, d;
        cout << "请输入第1个数 然后按回车" << endl;
        cin >> b;
        cout << "请输入第2个数 然后按回车" << endl;
        cin >> c;
        d = b - c;
        cout << "两个数的差是" << d << endl;
    }
}

5,int 一维数据的定义和使用

#include <iostream>
using namespace std;
int main()
{
    int x[3] = {5,3,2};
    cout << "数组中第1个数是" << x[0] << endl;
    cout << "数组中第2个数是" << x[1] << endl;
    cout << "数组中第3个数是" << x[2] << endl;
    cout << "第2和第3个数的和是" << x[1]+x[2] << endl;
    x[0] = 12;
    cout << "数组中第1个数 修改后是" << x[0] << endl;
}

6,++运算符的使用

#include <iostream>
using namespace std;
int main()
{
    int a=0;
    cout << a << endl;
    a++;
    cout << a << endl;
    a++;
    cout << a << endl;
    a++;
    cout << a << endl;
    a++;
    cout << a << endl;
    a++;
    cout << a << endl;
    a++;
    cout << a << endl;
    a++;
    cout << a << endl;
    a++;
}

7,for 循环语句的使用

#include <iostream>
using namespace std;
int main()
{
    for (int a=0;a<=100;a++)
    {
        cout << a << endl;
    }
}

8,while循环语句的使用

#include <iostream>
using namespace std;
int main()
{
    int a = 0;
    while (a<=100)
    {
        cout << a << endl;
        a++;
    }
}

9,无返回值 函数的定义和使用

#include <iostream>
using namespace std;
void 输出()
{
    cout << "fdsafs" << endl;
}
void 循环输出()
{
    for (int a = 0; a <= 100; a++)
    {
        cout << a << endl;
    }
}
int main()
{
    输出();
    循环输出();
}

10,返回值 函数的定义和使用

#include <iostream>
using namespace std;
void 输出()
{
    cout << "fdsafs" << endl;
}
int 加法(int a, int b)
{
    int x;
    x = a + b;
    return x;
}
int 减法(int a, int b)
{
    int x;
    x = a - b;
    return x;
}
int main()
{
    输出();
    int x;
    x = 加法(2, 3);
    cout << "x=" << x << endl;
}

11,C++ 类

#include <iostream>
class A
{
    int a;//默认是私有数据,类外部无法调用 
private: //私有的 数据
    int b;
protected://protected 是介于 private 和 public 之间的一种可见性修饰符。protected 成员既不能被外部对象直接访问,但可以被派生类(继承类)访问
    int c;
public:  //公共的 数据
    int d;
    void ax()
    {
        a = 1008;
        std::cout << "ax函数:" << std::endl;
    }
};
int main()
{
 
    A m;
    m.d = 10;
    std::cout << m.d << std::endl;
    m.ax();
}

12,C++语法之类的继承一

#include <iostream>
class A
{
public:
    A(){
        a = 10;
        b = 20;
    }
 
    int a, b; 
 
    void ax()
    {
        std::cout << "ax函数:"<< std::endl;
    }
};
class B :public A
{
public:
    void bx()
    {
        std::cout << "bx函数:" << std::endl;
    }
};
 
int main()
{
    A m;
    m.ax();
 
    B n;
    n.ax();
    n.bx();
}

13,C++类的构造函数和析构函数

#include <iostream>
class A
{
public:
    int a;
    A()
    {
        a = 0;
        std::cout << "A的 不带参数的 构造函数:" << std::endl;
    }
    A(int b)
    {
        a = b;
        std::cout << "A的 带参数的 构造函数:" << std::endl;
    }
    ~A()
    {
        std::cout << "A的 析构函数:" << std::endl;
    }
    void ax()
    {
        std::cout << "ax函数:" << std::endl;
    }
 
};
int main()
{
    std::cout << "测试一\n\n\n";
 
    A m;
    m.ax();
 
    std::cout << "\n\n测试二\n\n";
 
    A n(2);
    n.ax();
}

14,C++语法之类的继承中的构造函数

#include <iostream>
class A
{
public:
    A()
    {
        std::cout << "A的 构造函数:" << std::endl;
    }
    ~A()
    {
        std::cout << "A的 析构函数:" << std::endl;
    }
    void ax()
    {
        std::cout << "ax函数:" << std::endl;
    }
 
};
class B :public A
{
public:
    B()
    {
        std::cout << "B的 构造函数:" << std::endl;
    }
    ~B()
    {
        std::cout << "B的 析构函数:" << std::endl;
    }
};
 
int main()
{
    B m;
    m.ax();
}

15,指针的定义和使用

#include <iostream>
using namespace std;
int main()
{
    int a = 10;
    int* p;
    p = &a;
    cout << "a=" << a << endl;
    cout << "&a=" << &a << endl;
    cout << "p=" << p << endl;
    cout << "*p=" << *p << endl;
 
    *p = 20;
    cout << "a=" << a << endl;
    cout << "&a=" << &a << endl;
    cout << "p=" << p << endl;
    cout << "*p=" << *p << endl;
 
}

16,C++语法之类的this指针

#include <iostream>
class A
{
public:
    A()
    {
        a = 10;
        b = 20;
    }
 
    int a, b; 
 
    void add(int a, int b)
    {
        int c= a + b;
        std::cout << "add:"<<c << std::endl;
    }
 
    void add2(int a, int b)
    {
        int c = this->a + this->b;
        std::cout << "add2:" << c << std::endl;
    }
 
    void add3(int a, int b)
    {       
        std::cout << "add3:" << std::endl;
        this->add(a,b);
        this->add2(a,b);
    }
};
 
int main()
{
    A m;
    m.add(2, 3);
    m.add2(2, 3);
    m.add3(2, 3);
}

17,C++语法之命名空间一

#include <iostream>
namespace a
{
	void 输出()
	{
		std::cout << "命名空间a里的输出函数" << std::endl;
	}
};
namespace b
{
	void 输出()
	{
		std::cout << "命名空间b里的输出函数" << std::endl;
	}
};
using namespace a;//这里引入命名空间
int main()
{
    输出();
}
#include <iostream>
namespace a
{
	void 输出()
	{
		std::cout << "命名空间a里的输出函数" << std::endl;
	}
};
namespace b
{
	void 输出()
	{
		std::cout << "命名空间b里的输出函数" << std::endl;
	}
};
 
int main()
{
    a::输出();//用命名空间调用函数
}

18,C++语法之命名空间二

A.h头文件中代码:

namespace a
{
    void 输出();
};


A.cpp源文件中代码:

#include <iostream>
#include "A.h"
void a::输出()
{
    std::cout << "A.h里的输出函数" << std::endl;
}


B.h头文件中代码:

namespace b
{
    void 输出();
};


B.cpp源文件中代码:

#include <iostream>
#include "B.h"
void b::输出()
{
    std::cout << "B.h里的输出函数" << std::endl;
}


主函数所在源文件代码1:引入命名空间 b

#include <iostream>
#include "A.h"
#include "B.h"
using namespace b;
int main()
{
    输出();
}


运行结果 : B.h里的输出函数

19,char变量和char数组和字符串

#include <iostream>
using namespace std;
int main()
{
    char a = 'x';
    cout << "a的值为" << a << endl;
    char x[3] = { 'a','b','c' };
    cout << "x[0]=" << x[0] << endl;
    cout << "x[1]=" << x[1] << endl;
    cout << "x[2]=" << x[2] << endl;
 
    char y[3] = { 'a','b','\0' };
    cout << "y[0]=" << y[0] << endl;
    cout << "y[1]=" << y[1] << endl;
    cout << "y[2]=" << y[2] << endl;
 
    cout << "y的值为" << y << endl;
    cout << "x的值为" << x << endl;
}

20,字符串相关函数

#include <iostream>
#include <cstring>
 
using namespace std;
 
int main ()
{
   char str1[13] = "runoob";
   char str2[13] = "google";
   char str3[13];
   int  len ;
 
   // 复制 str1 到 str3
   strcpy( str3, str1);
   cout << "strcpy( str3, str1) : " << str3 << endl;
 
   // 连接 str1 和 str2
   strcat( str1, str2);
   cout << "strcat( str1, str2): " << str1 << endl;
 
   // 连接后,str1 的总长度
   len = strlen(str1);
   cout << "strlen(str1) : " << len << endl;
 
   return 0;
}

21,结构体struct

#include <iostream>
#include <cstring>
 
using namespace std;
 
// 声明一个结构体类型 Books 
struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
int main( )
{
   Books Book1;        // 定义结构体类型 Books 的变量 Book1
   Books Book2;        // 定义结构体类型 Books 的变量 Book2
 
   // Book1 详述
   strcpy( Book1.title, "C++ 教程");
   strcpy( Book1.author, "Runoob"); 
   strcpy( Book1.subject, "编程语言");
   Book1.book_id = 12345;
 
   // Book2 详述
   strcpy( Book2.title, "CSS 教程");
   strcpy( Book2.author, "Runoob");
   strcpy( Book2.subject, "前端技术");
   Book2.book_id = 12346;
 
   // 输出 Book1 信息
   cout << "第一本书标题 : " << Book1.title <<endl;
   cout << "第一本书作者 : " << Book1.author <<endl;
   cout << "第一本书类目 : " << Book1.subject <<endl;
   cout << "第一本书 ID : " << Book1.book_id <<endl;
 
   // 输出 Book2 信息
   cout << "第二本书标题 : " << Book2.title <<endl;
   cout << "第二本书作者 : " << Book2.author <<endl;
   cout << "第二本书类目 : " << Book2.subject <<endl;
   cout << "第二本书 ID : " << Book2.book_id <<endl;
 
   return 0;
}

22,C++ 数据类型

#include<iostream>  
#include <limits>
 
using namespace std;  
  
int main()  
{  
    cout << "type: \t\t" << "************size**************"<< endl;  
    cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);  
    cout << "\t最大值:" << (numeric_limits<bool>::max)();  
    cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;  
    cout << "char: \t\t" << "所占字节数:" << sizeof(char);  
    cout << "\t最大值:" << (numeric_limits<char>::max)();  
    cout << "\t\t最小值:" << (numeric_limits<char>::min)() << endl;  
    cout << "signed char: \t" << "所占字节数:" << sizeof(signed char);  
    cout << "\t最大值:" << (numeric_limits<signed char>::max)();  
    cout << "\t\t最小值:" << (numeric_limits<signed char>::min)() << endl;  
    cout << "unsigned char: \t" << "所占字节数:" << sizeof(unsigned char);  
    cout << "\t最大值:" << (numeric_limits<unsigned char>::max)();  
    cout << "\t\t最小值:" << (numeric_limits<unsigned char>::min)() << endl;  
    cout << "wchar_t: \t" << "所占字节数:" << sizeof(wchar_t);  
    cout << "\t最大值:" << (numeric_limits<wchar_t>::max)();  
    cout << "\t\t最小值:" << (numeric_limits<wchar_t>::min)() << endl;  
    cout << "short: \t\t" << "所占字节数:" << sizeof(short);  
    cout << "\t最大值:" << (numeric_limits<short>::max)();  
    cout << "\t\t最小值:" << (numeric_limits<short>::min)() << endl;  
    cout << "int: \t\t" << "所占字节数:" << sizeof(int);  
    cout << "\t最大值:" << (numeric_limits<int>::max)();  
    cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;  
    cout << "unsigned: \t" << "所占字节数:" << sizeof(unsigned);  
    cout << "\t最大值:" << (numeric_limits<unsigned>::max)();  
    cout << "\t最小值:" << (numeric_limits<unsigned>::min)() << endl;  
    cout << "long: \t\t" << "所占字节数:" << sizeof(long);  
    cout << "\t最大值:" << (numeric_limits<long>::max)();  
    cout << "\t最小值:" << (numeric_limits<long>::min)() << endl;  
    cout << "unsigned long: \t" << "所占字节数:" << sizeof(unsigned long);  
    cout << "\t最大值:" << (numeric_limits<unsigned long>::max)();  
    cout << "\t最小值:" << (numeric_limits<unsigned long>::min)() << endl;  
    cout << "double: \t" << "所占字节数:" << sizeof(double);  
    cout << "\t最大值:" << (numeric_limits<double>::max)();  
    cout << "\t最小值:" << (numeric_limits<double>::min)() << endl;  
    cout << "long double: \t" << "所占字节数:" << sizeof(long double);  
    cout << "\t最大值:" << (numeric_limits<long double>::max)();  
    cout << "\t最小值:" << (numeric_limits<long double>::min)() << endl;  
    cout << "float: \t\t" << "所占字节数:" << sizeof(float);  
    cout << "\t最大值:" << (numeric_limits<float>::max)();  
    cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;  
    cout << "size_t: \t" << "所占字节数:" << sizeof(size_t);  
    cout << "\t最大值:" << (numeric_limits<size_t>::max)();  
    cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;  
    cout << "string: \t" << "所占字节数:" << sizeof(string) << endl;  
    // << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;  
    cout << "type: \t\t" << "************size**************"<< endl;  
    return 0;  
}

相关文章:

  • Spring 循环依赖
  • python并发爬虫
  • 基于Spring Boot的个性化商铺系统的设计与实现(LW+源码+讲解)
  • 数据结构day04
  • 爱普生VG3225EFN压控晶振5G基站低噪声的解决方案
  • windows下面nginx配置及测试
  • 网络安全之vlan实验
  • 接口/UI自动化面试题
  • Springboot整合elasticsearch详解 封装模版 仓库方法 如何在linux里安装elasticsearch
  • 八股——Mysql篇
  • WebAssembly实践,性能也有局限性
  • 小白工具PDF转换 PDF转图片 超便捷软件 文件格式转换 简单好用效率高
  • 新手村:逻辑回归-理解04:熵是什么?
  • 第五天 开始Unity Shader的学习之旅之Unity中的基础光照之漫反射光照模型
  • 座舱网联融合新旗舰!移远通信48 TOPS座舱方案携AI大模型能力,赋能多域融合
  • LabVIEW时间触发协议
  • husky的简介以及如果想要放飞自我的解决方案
  • CCF-GESP 等级考试 2025年3月认证C++一级真题解析
  • 一文解读DeepSeek在工业制造领域的应用
  • Win32 / C++ ini配置文件解析类(支持简易加解密)
  • 如何制作免费永久网站/商务网站建设
  • 电商型网站开发多少钱/创意广告
  • 导航网站cms/公众号引流推广平台
  • 深圳网站建设亿联时代/微信营销软件有哪些
  • 什么网站做新闻更好/seo自然排名
  • 如何防止网站被劫持/百度关键词排名手机