Professional C++ 01 A Crash Course in C++ 快速的C++基础知识复习

该章节回顾了C++的基础知识,包括空间命名规则、变量定义与操作符、循环语句、枚举类型等内容,并通过一个管理员工系统的例子进行实践应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本章节主要是对C++基础知识的回顾,不会涉及太简单的知识点,比如=和==的区别,也不会涉及太复杂的知识,比如volatile关键字的作用。

(1) 空间命名规则

#ifndef NAMESPACE_H
#define NAMESPACE_H
// namespaces.h
namespace mycode {
    void foo();
}
#endif // NAMESPACE_H

#include "namespace.h"
#include <iostream>

void mycode::foo()
{
    std::cout << "foo() called in the mycode namespace" << std::endl;
}

#include "namespace.h"

using namespace std;
using namespace mycode;

int main(int argc, char *argv[])
{
    foo();
}

output:

foo() called in the mycode namespace

(2) 变量的定义, 操作符,循环语句全部都是基础知识。

(3) 枚举类型

typedef enum { kPieceTypeKing, kPieceTypeQueen, kPieceTypeRook,
	kPieceTypePawm
} PieceT;

(4) Stack 和 Heap的区别

stack分配的空间一般都是程序自动管理,heap用于new分配,delete删除。

(5)综合举例,一个管理员工系统

#ifndef EMPLOYEE_H
#define EMPLOYEE_H
// employee.h
#include <iostream>
namespace Records {
    const int kDefaultStartingSalary = 30000;

    class Employee {
    public:
        Employee();
        void promote(int inRaiseAmount = 1000);
        void demote(int inDemeritAmount = 1000);
        void hire();
        void fire();
        void display();

        void setFirstName(std::string inFirstName);
        std::string getFirstName();
        void setLastName(std::string inLastName);
        std::string  getLastName();
        void setEmployeeNumber(int inEmployeeNumber);
        int getEmployeeNumber();
        void setSalary(int inNewSalary);
        int getSalary();
        bool getIsHired();

    private:
        std::string mFirstName;
        std::string mLastName;
        int mEmployeeNumber;
        int mSalary;
        bool fHired;
    };
}

#endif // EMPLOYEE_H

#include "employee.h"
#include <iostream>
using namespace std;
namespace Records {

Employee::Employee()
{
    mFirstName = "";
    mLastName = "";
    mEmployeeNumber = -1;
    mSalary = kDefaultStartingSalary;
    fHired = false;
}

void Employee::promote(int inRaiseAmount)
{
    setSalary(getSalary() + inRaiseAmount);
}

void Employee::demote(int inDemeritAmount)
{
    setSalary(getSalary() - inDemeritAmount);
}

void Employee::hire()
{
    fHired = true;
}

void Employee::fire()
{
    fHired = false;
}

void Employee::display()
{
    cout << "Employee: " << getLastName() << ", " << getFirstName() << endl;
    cout << "-------------------------" << endl;
    cout << (fHired ? "Current Employee" : "Former Employee") << endl;
    cout << "Employee Nmber: " << getEmployeeNumber() << endl;
    cout << "Salary: $" << getSalary() << endl;
    cout << endl;
}

void Employee::setFirstName(std::__cxx11::string inFirstName)
{
    mFirstName = inFirstName;
}

std::__cxx11::string Employee::getFirstName()
{
    return mFirstName;
}

void Employee::setLastName(std::__cxx11::string inLastName)
{
    mLastName = inLastName;
}

std::__cxx11::string Employee::getLastName()
{
    return mLastName;
}

void Employee::setEmployeeNumber(int inEmployeeNumber)
{
    mEmployeeNumber = inEmployeeNumber;
}

int Employee::getEmployeeNumber()
{
    return mEmployeeNumber;
}

void Employee::setSalary(int inNewSalary)
{
    mSalary = inNewSalary;
}

int Employee::getSalary()
{
    return mSalary;
}

bool Employee::getIsHired()
{
    return fHired;
}
}

#ifndef DATABASE_H
#define DATABASE_H
#include "employee.h"
#include <iostream>
// Database.h
namespace Records {
    const int kMaxEmployees =100;
    const int kFirstEmployeeNumber = 1000;

    class Database {
    public:
        Database();
        ~Database();

        Employee &addEmployee(std::string inFirstName, std::string inLastName);
        Employee &getEmployee(int inEmployeeNumber);
        Employee &getEmployee(std::string inFirstName, std::string inLastName);

        void displayAll();
        void displayCurrent();
        void displayFormer();
    protected:
        Employee mEmployees[kMaxEmployees];
        int mNextSlot;
        int mNextEmployeeNumber;
    };
}

#endif // DATABASE_H

// Database.cpp

#include <iostream>
#include <stdexcept>
#include "database.h"

using namespace std;

namespace Records {
    Database::Database()
    {
        mNextSlot = 0;
        mNextEmployeeNumber = kFirstEmployeeNumber;
    }

    Database::~Database()
    {

    }

    Employee &Database::addEmployee(string inFirstName, string inLastName)
    {
        if (mNextSlot >= kMaxEmployees) {
            cerr << "There is no more room to add the new employee!" << endl;
            throw exception();
        }

        Employee &theEmployee = mEmployees[mNextSlot++];
        theEmployee.setFirstName(inFirstName);
        theEmployee.setLastName(inLastName);
        theEmployee.setEmployeeNumber(mNextEmployeeNumber++);
        theEmployee.hire();

        return theEmployee;
    }

    Employee &Database::getEmployee(int inEmployeeNumber)
    {
        for (int i = 0; i < mNextSlot; i++) {
            if (mEmployees[i].getEmployeeNumber() == inEmployeeNumber) {
                return mEmployees[i];
            }
        }
        cerr << "No empolyee with employee number " << inEmployeeNumber << endl;
        throw exception();
    }

    Employee &Database::getEmployee(string inFirstName, string inLastName)
    {
        for (int i = 0; i < mNextSlot; i++) {
            if (mEmployees[i].getFirstName() == inFirstName &&
                    mEmployees[i].getLastName() == inLastName) {
                return mEmployees[i];
            }
        }
        cerr << "No match with name " << inFirstName << " " << inLastName << endl;
    }

    void Database::displayAll()
    {
        for (int i = 0; i < mNextSlot; i++) {
            mEmployees[i].display();
        }
    }

    void Database::displayCurrent()
    {
        for (int i = 0; i < mNextSlot; i++) {
            if (mEmployees[i].getIsHired()) {
                mEmployees[i].display();
            }
        }
    }

    void Database::displayFormer()
    {
        for (int i = 0; i < mNextSlot; i++) {
            if (!mEmployees[i].getIsHired()) {
                mEmployees[i].display();
            }
        }
    }
}

#include <iostream>
#include "database.h"

using namespace std;
using namespace Records;

int main(int argc, char *argv[])
{
    Database myDB;
    Employee &emp1 = myDB.addEmployee("Greg", "Wallis");
    emp1.fire();

    Employee &emp2 = myDB.addEmployee("Scott", "Kleper");
    emp2.setSalary(100000);

    Employee &emp3 = myDB.addEmployee("Nick", "Solter");
    emp3.setSalary(10000);
    emp3.promote();

    cout << "all employee: " << endl;
    cout << "current employee: " << endl;
    cout << endl;
    myDB.displayCurrent();

    cout << endl;
    cout << "former employee: " << endl;
    cout << endl;
    myDB.displayFormer();
}

output:

all employee:

current employee:


Employee: Kleper, Scott

-------------------------

Current Employee

Employee Nmber: 1001

Salary: $100000


Employee: Solter, Nick

-------------------------

Current Employee

Employee Nmber: 1002

Salary: $11000



former employee:


Employee: Wallis, Greg

-------------------------

Former Employee

Employee Nmber: 1000

Salary: $30000

Author: Josh Lospinoso Pub Date: 2019 ISBN: 978-1593278885 Pages: 792 Language: English Format: PDF Size: 10 Mb A fast-paced, thorough introduction to modern C++ written for experienced programmers. After reading C++ Crash Course, you’ll be proficient in the core language concepts, the C++ Standard Library, and the Boost Libraries. C++ is one of the most widely used languages for real-world software. In the hands of a knowledgeable programmer, C++ can produce small, efficient, and readable code that any programmer would be proud of. Designed for intermediate to advanced programmers, C++ Crash Course cuts through the weeds to get you straight to the core of C++17, the most modern revision of the ISO standard. Part 1 covers the core of the C++ language, where you’ll learn about everything from types and functions, to the object life cycle and expressions. Part 2 introduces you to the C++ Standard Library and Boost Libraries, where you’ll learn about all of the high-quality, fully-featured facilities available to you. You’ll cover special utility classes, data structures, and algorithms, and learn how to manipulate file systems and build high-performance programs that communicate over networks. You’ll learn all the major features of modern C++, including: Fundamental types, reference types, and user-defined types The object lifecycle including storage duration, memory management, exceptions, call stacks, and the RAII paradigm Compile-time polymorphism with templates and run-time polymorphism with virtual classes Advanced expressions, statements, and functions Smart pointers, data structures, dates and times, numerics, and probability/statistics facilities Containers, iterators, strings, and algorithms Streams and files, concurrency, networking, and application development With well over 500 code samples and nearly 100 exercises, C++ Crash Course is sure to help you build a strong C++ foundation.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值