GithubHelp home page GithubHelp logo

study-notes-cpp's Introduction

Study-Notes-C-

C++学习札记

study-notes-cpp's People

Watchers

 avatar

study-notes-cpp's Issues

其它知识点

  1. 如果函数无需改变引用形参的值,最好将其声明为常量引用。
  2. 类 是一种数据结构,对象是一个类的实例,比如classname p;p即是classname类型的对象。对象是类的实例,类是对象的模板。
  3. 拷贝初始化与直接初始化
    • 拷贝初始化:使用等号(=)初始化一个变量,编译器把等号右侧的初始值拷贝到新创建的对象中去。
      拷贝初始化不仅在使用=定义变量时会发生,下列情况也会:
      • 将一个对象作为实参传递给一个非引用类型的形参
      • 从一个返回类型为非引用类型的函数返回一个对象
      • 用花括号列表初始化一个数组中的元素或一个聚合类(wey: 类似C结构体)中的成员
    • 与之相反,如果不使用等号,则执行的是直接初始化。
string dots(10, '.');  // 直接初始化
string s(dots);  // 直接初始化
string s1("hello");  // 直接初始化
string s2 = dots;  // 拷贝初始化
string null_book = "9-999-99999-9";  // 拷贝初始化
string nines = string(100, '9');  // 拷贝初始化
  1. 头文件后缀:通常使用 .h 作为头文件的后缀,但也有一些程序员习惯 .H、.hpp 或 .hxx。标准库头文件通常不带后缀。编译器一般不关心头文件名的形式,担忧的 IDE 对此有特定要求。
  2. char, signed char, unsigned char三者区别:不同于int等同于 signed int,char不等同于signed char,char会表现其中一种,具体由编译器决定。在一些编译器中char被定义为signed char, 一些被定义为unsigned char。
  3. 如何选择数据类型
    如何选择数据类型
  4. 请勿混用带符号类型和无符号类型
#include <iostream>
using namespace std;

int main()
{
	unsigned u = 10, u2 = 42;
	std::cout << u2 - u << std::endl; // 32
	std::cout << u - u2 << std::endl; // 4294967264
	
	int i = 10, i2 = 42;
	std::cout << i2 - i << std::endl; // 32
	std::cout << i - i2 << std::endl; // -32
	std::cout << i - u << std::endl; // 0
	std::cout << u - i << std::endl; // 0
	
	unsigned int uu = 10;
	int ii = -42;
	std::cout << ii + ii << std::endl; // -84
	std::cout << uu + ii << std::endl; // 4294967264,既有int又有无符号时,int值会转为无符号,
	
	return 0;
}
  1. 指定字面值的类型
    指定字面值的类型
  2. 变量声明和定义的关系
    变量声明和定义的关系
  3. 命名空间的using声明
#include <iostream>

int main() {
    int i;
    std::cin >> i;
    std::cout << "hello" << std::endl;
}

#include <iostream>
using std::cout; 
using std::cin;
using std::endl;

int main() {
    int i;
    cin >> i;
    cout << "hello" << endl;
}

#include <iostream>
using namespace std;

int main() {
    int i;
    cin >> i;
    cout << "hello" << endl;
}

<iostream>标准输入输出

  1. endl:操纵符manipulator,效果是结束当前行,并将与设备关联的缓冲区中的内容刷到设备中。

类 & 对象

Constructors in C++ | GeeksforGeeks

类的构造函数

What is constructor?

A constructor is a special type of member function of a class which initializes objects of a class. In C++, Constructor is automatically called when object(instance of class) create. It is special member function of the class because it does not have any return type.

How constructors are different from a normal member function?

A constructor is different from normal functions in following ways:

  • Constructor has same name as the class itself
  • Constructors don’t have return type
  • A constructor is automatically called when an object is created.
  • It must be placed in public section of class.
  • If we do not specify a constructor, C++ compiler generates a default constructor for object (expects no parameters and has an empty body).

1. Default Constructors: Default constructor is the constructor which doesn’t take any argument. It has no parameters.

// Cpp program to illustrate the
// concept of Constructors
#include <iostream>
using namespace std;

class construct
{
public:
	int a, b;

	// Default Constructor
	construct()
	{
		a = 10;
		b = 20;
	}
};

int main()
{
	// Default constructor called automatically
	// when the object is created
	construct c;
	cout << "a: " << c.a << endl
		<< "b: " << c.b;
	return 1;
}

Note: Even if we do not define any constructor explicitly, the compiler will automatically provide a default constructor implicitly.

2. Parameterized Constructors: It is possible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created. To create a parameterized constructor, simply add parameters to it the way you would to any other function. When you define the constructor’s body, use the parameters to initialize the object.
默认的构造函数没有任何参数,但如果需要,构造函数也可以带有参数。这样在创建对象时就会给对象赋初始值,如下面的例子所示:

// CPP program to illustrate
// parameterized constructors
#include <iostream>
using namespace std;

class Point
{
private:
	int x, y;

public:
	// Parameterized Constructor
	Point(int x1, int y1)
	{
		x = x1;
		y = y1;
	}

	int getX()
	{
		return x;
	}
	int getY()
	{
		return y;
	}
};

int main()
{
	// Constructor called
	Point p1(10, 15);

	// Access values assigned by constructor
	cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();

	return 0;
}

构造函数重载
In C++, We can have more than one constructor in a class with same name, as long as each has a different list of arguments.This concept is known as Constructor Overloading and is quite similar to function overloading.

  • Overloaded constructors essentially have the same name (exact name of the class) and differ by number and type of arguments.
  • A constructor is called depending upon the number and type of arguments passed.
  • While creating the object, arguments must be passed to let compiler know, which constructor needs to be called.
// C++ program to illustrate
// Constructor overloading
#include <iostream>
using namespace std;

class construct
{

public:
	float area;
	
	// Constructor with no parameters
	construct()
	{
		area = 0;
	}
	
	// Constructor with two parameters
	construct(int a, int b)
	{
		area = a * b;
	}
	
	void disp()
	{
		cout<< area<< endl;
	}
};

int main()
{
	// Constructor Overloading
	// with two different constructors
	// of class name
	construct o;
	construct o2( 10, 20);
	
	o.disp();
	o2.disp();
	return 1;
}

3. Copy Constructor: 在后面评论中单独描述

使用初始化列表来初始化字段

使用初始化列表来初始化字段:

Line::Line( double len): length(len)
{
    cout << "Object is being created, length = " << len << endl;
}

上面的语法等同于如下语法:

Line::Line( double len)
{
    length = len;
    cout << "Object is being created, length = " << len << endl;
}

假设有一个类 C,具有多个字段 X、Y、Z 等需要进行初始化,同理地,您可以使用上面的语法,只需要在不同的字段使用逗号进行分隔,如下所示:

C::C( double a, double b, double c): X(a), Y(b), Z(c)
{
  ....
}

类的析构函数

类的析构函数是类的一种特殊的成员函数,它会在每次删除所创建的对象时执行

析构函数的名称与类的名称是完全相同的,只是在前面加了个波浪号(~)作为前缀,它不会返回任何值,也不能带有任何参数。析构函数有助于在跳出程序(比如关闭文件、释放内存等)前释放资源。

#include <iostream>
 
using namespace std;
 
class Line
{
   public:
      void setLength( double len );
      double getLength( void );
      Line();   // 这是构造函数声明
      ~Line();  // 这是析构函数声明
 
   private:
      double length;
};
 
// 成员函数定义,包括构造函数
Line::Line(void)
{
    cout << "Object is being created" << endl;
}
Line::~Line(void)
{
    cout << "Object is being deleted" << endl;
}
 
void Line::setLength( double len )
{
    length = len;
}
 
double Line::getLength( void )
{
    return length;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.