博客
关于我
【C++】谭浩强《C++面向对象程序设计》-学习笔记
阅读量:553 次
发布时间:2019-03-09

本文共 1427 字,大约阅读时间需要 4 分钟。

C++开发入门基础知识

1. 输出与输入

C++是一种通用编程语言,由Bjarne Stroustrup在1983年首创。语言特点强调面向对象编程和标准化。

1.1 输出机制

C++程序的输出主要依赖于<iostream>头文件。常用的输出流对象包括cout,而<<为插入运算符。例如:

#include 
using namespace std; // 使用标准命名空间int main() { cout << "Hello, World!" << endl; return 0;}

endl控制符与\n功能相同,用于换行。

1.2 输入机制

输入流对象为cin,提取运算符为>>。例如:

int a, b;cin >> a >> b;

输入值间需使用空格或换行符分隔。

2. 数据类型与常量

2.1 常量定义

使用const定义常量,确保其不可修改。例如:

const int MAX_VALUE = 100;

3. 类与对象

3.1 类的初始化

类的数据成员在定义时不能直接初始化。只有公有数据成员可以在对象定义时初始化。例如:

class Time {public:    Time(int hour, int minute, int sec) {        hour = 0; minute = 0; sec = 0;    }};Time t1 = {14, 56, 30};

3.2 构造函数

构造函数的作用是初始化对象。无参数构造函数默认生成,无法修改成员变量值。带参数构造函数可实现不同对象的个性化初始化。例如:

class Box {public:    Box(int h = 0, int w = 0, int len = 0) : height(h), width(w), length(len) {}};

3.3 析构函数

析构函数用于释放资源。每个类只能有一个析构函数,且不能返回值。例如:

~Box() {    // 释放资源}

4. 指针与内存管理

4.1 指针基础

指针存储对象地址,使用*解除引用。例如:

int jumbo = 23;int *pe = &jumbo;

4.2 指针应用

通过指针访问对象成员或函数。例如:

int *p1 = &t1.hour;void (Time::*p2)() = &Time::get_time;t1.*p2();

5. 常用注意事项

5.1 编译器错误处理

  • 非静态成员引用需与对象结合。
  • switch语句需在case后加{}

6. 字符串处理

使用<string>头文件定义字符串变量。例如:

#include 
string str = "Hello";string another = "World";

7. 模板功能

定义通用函数或类。例如:

template 
class Util {public: static T create(T &obj) { // 通用创建逻辑 }};

8. 标准库使用

包括必要的头文件,正确使用控制符如setw进行格式化输出。例如:

#include 
cout << setw(5) << "Name:" << name << endl;

通过以上知识,您可以快速入门C++编程,逐步掌握面向对象编程的核心概念。

转载地址:http://drwsz.baihongyu.com/

你可能感兴趣的文章
ProcessOnLoading
查看>>
SpringBoot中集成screw(螺丝钉)实现数据库表结构文档生成
查看>>
PROFINET 模拟器使用教程
查看>>
Program type already present: android.support.v4.widget.EdgeEffectCompat
查看>>
PyTorch中文版官方教程来啦(附下载)
查看>>
Progress Kemp LoadMaster 远程命令执行漏洞复现(CVE-2024-1212)
查看>>
Project configuration is not up-to-date with pom.xml. Run Maven->Update Project
查看>>
Project Euler 15 Lattice paths
查看>>
Project Euler 48 Self powers( 大数求余 )
查看>>
Project Euler Problem 12: Highly divisible triangular number
查看>>
ProjectEuler 2
查看>>
projection介绍及EPSG:4326和EPSG:3857的投射转换
查看>>
project打开文件时,显示无法识别此文件格式?
查看>>
Prometheus + Grafana on Kubernetes部署
查看>>
prometheus + grafana进行服务器资源监控
查看>>
Prometheus Alertmanager 告警配置详解
查看>>
Prometheus Grafana 展示平台
查看>>
Prometheus pushgateway使用详解
查看>>
Prometheus 云原生 - Prometheus 数据模型、Metrics 指标类型、Exporter 相关
查看>>
Prometheus 云原生 - 基于 file_sd、http_sd 实现 Service Discovery
查看>>