cout

重载与多态

…衆ロ難τιáo~ 提交于 2019-12-02 13:07:53
多态的类型 :分为4类,重载多态,强制多态,包含多态,参数多态。 以前所学过的普通函数的重载也属于重载多态。强制多态是指将一个变元的类型加以变化,以符合一个函数或操作的要求,比如int型与float型相加,要先进行类型转换。 多态的实现 :分为两类,编译时的多态与运行时的多态。 前者在编译的过程中确定了同名的具体操作对象,而后者是在程序运行过程中才多态地确定操作所指向的对象。这种确定操作具体对象的过程就是绑定。绑定工作在编译连接阶段完成的情况为 静态绑定 ,在程序运行过程中完成的情况是 动态绑定 。 运算符重载 运算符重载时对已有的运算符赋予多重含义,使同一个运算符作用于不同的数据类型时有不同的行为。 重载形式 重载为类的非静态成员函数,重载为非成员函数。 两种重载方式的声明语法形式一般相同 都为 返回类型 operator 运算符(形参表) { 函数体 } 非成员函数一般定义为友元函数。 **实现‘+’的重载** #include<iostream> using namespace std; class counter { private: float a; public: counter(float a=0):a(a){} counter operator+(counter& c)const { **//定义为类的非静态成员函数** return counter(a + c.a

Cout of a string is giving an error and a hard time some insight help pls?

末鹿安然 提交于 2019-12-02 12:02:19
I cant find the error in this piece of code could anyone please insight me? I ran the debugging but the errors are un-understandable.. #include "stdafx.h" #include <iostream> using namespace std; int main() { string name; cout << "Input your name please?" << endl; cin >> name; if { (name == "Bart Simpson") cout << "You have been very naughty" << endl; } return 0; } Problems: You have some missing #include s, which probably caused your initial compiler errors. You have a simple syntax error with your if statement. Using the stream extraction operator will never yield a string with whitespace

c++第五次作业

亡梦爱人 提交于 2019-12-02 11:26:08
重载与多态 一.运算符重载 定义:运算符重载是对已有运算符赋予多重含义,使同一个运算符作用于不同类型的数据时导致不同的行为。例如: 在这样一段程序中,我们对a,b进行加法运算, # include < iostream > using namespace std; class Counter {public : Counter() {}; Counter(int i) { count = i; } private : int count; }; int main() { Counter a(1),b(2),c; cout << "a=" << a<<endl; cout << "b=" << b << endl; c = a + b; cout << "c=" <<c << endl; system("pause"); return 0; } 会出现这样的错误 这时候就需要运算符重载 1.1 运算符重载的规则:(1)c++中的运算符大多可以重载,而且只能重载已有运算符。 (2)重载之后运算符优先级和结合性都不会改变。 (3)运算符重载是针对新类型数据的实际需要,对原有运算符进行适当的改造。 运算符重载为类的成员函数的语法形式: 返回类型 operater 运算符{形参表} { 函数体 } 以非成员函数重载的语法与之相同,但有时访问涉及到私有成员,可以将函数声明为友元函数。例如: #

std::cout doen't like std::endl and string in conditional-if

扶醉桌前 提交于 2019-12-02 11:01:25
main.cpp: In function ‘void PrintVector(std::vector<std::__cxx11::basic_string<char> >&, bool)’: main.cpp:16:41: error: overloaded function with no contextual type information std::cout << ((newline)? (std::endl) : ""); ^~ Why std::cout doen't like std::endl and string in conditional-if? std::endl is a stream manipulator. It's a function. It does not have a common type with "" . So they cannot be the two types of a conditional expression. Since the common type is the type of the whole expression. You probably don't even need everything std::endl does besides adding a new line, so just replace

c++11 regex

时光怂恿深爱的人放手 提交于 2019-12-02 10:58:08
c++ regex 库,通过 #include <regex> 来使用,是c++11标准引入的功能。 regex 库提供三个最基本的正则表达式函数 regex_match -> 完全匹配 regex_search -> 局部匹配 regex_replace -> 匹配后替换 regex 有几个基本的类(其实都是模板类) : basic_regex -> 存储正则表达式 match_results -> 存储匹配结果们 sub_match -> 一个独立的比对结果,可以无感觉转化为字符串 regex_iterator -> 迭代执行regex_search的语法糖 basic_regex 通过传入正则表达式来构建,默认是ECMAScript格式,也支持其他的比如POSIX 格式,grep 格式等,其他格式需要通过第二个参数指定就是。 // explicit basic_regex ( const charT* str, flag_type flags = ECMAScript ); std : : regex seventh ( "[0-9A-Z]+" , std : : regex : : ECMAScript ) ; match_results 存储匹配结果(也就是n个sub_match),如果匹配失败,就是空的,否则,数组的第0位置是完整的匹配原始字符串

C++简介和基本语法(1章/2章)

独自空忆成欢 提交于 2019-12-02 10:49:50
C++简介 C++包含了三种编程模式,分别是面向过程、面向对象和泛型编程。 程序执行过程 C++编程格式 C++每个程序都会有main()函数作为入口,基本编程包括预处理/头文件、函数、输入输出语句等。 预处理和头文件 当需要引入外部文件时,需要引入头文件,引入头文件的格式有很多种,如需要使用isotream文件中的函数,可以书写问如#include<iostream>,#include” iostream” 两者区别在于: include""首先在当前目录下寻找,如果找不到,再到系统目录中寻找 include <>直接去系统目录中找 传统的C语言头文件引入时还附带.h扩展名,如include<math.h>,C++依然可以如此引用。对于存粹的C++头文件(如iostream),引入时不带扩展名。 输入输出 C++输入输出函数包含在iostream文件中的std命名空间内。 命名空间有两种使用方式, 方式一:在头文件下方输入using namespace std; 方式二:在函数前使用std::前缀的方式。 C++输出 Cout函数,cout函数中对<<运算符进行了重载,采用<<表示将输出数据插入cout输出流中,统一输出。 C++输出语句中特殊字符: Endl:换行 “\n”:换行 为了防止调试屏幕一闪而过,可以在函数末尾添加system("pause"); C++输入 C+

Can not print out the argv[] values using std::cout in VC++

半城伤御伤魂 提交于 2019-12-02 09:01:34
问题 This is my first question on the site even though i have been coming here for reference for quite some time now. I understand that argv[0] stores the name of the program and the rest of the commandline arguements are stored in teh remaining argv[k] slots. I also understand that std::cout treats a character pointer like a null terminated string and prints the string out. Below is my program. #include "stdafx.h" #include <fstream> #include <iostream> using namespace std; int _tmain(int argc,

How to redirect cout to console in linux?

时光总嘲笑我的痴心妄想 提交于 2019-12-02 08:18:10
I am writing a program which is a part of another program. In the main program, they redirect the default direction of cout to a LOG file. For debugging of my own programm, I need to redirect the output of cout to console (terminal) in linux. I cannot save the console rdbuf like the method described in the example at: http://www.cplusplus.com/reference/iostream/ios/rdbuf/ Is there any way to get the handle to the console of linux in c++ for my purpose? You need to define what you mean by the 'console' and what you mean by 'redirect'. If you're running a program in some context where its output

Educational Codeforces Round 75

▼魔方 西西 提交于 2019-12-02 07:50:25
目录 Contest Info Solutions A. Broken Keyboard B. Binary Palindromes C. Minimize The Integer D. Salary Changing E2. Voting (Hard Version) Contest Info Practice Link Solved A B C D E1 E2 F 6/7 O O O O O O - O 在比赛中通过 Ø 赛后通过 ! 尝试了但是失败了 - 没有尝试 Solutions A. Broken Keyboard 题意: 有一个打字机,如果某个按键是好的,那么按下那个按键之后会在打字槽中追加一个该字符,如果是坏的则会追加两个。 现在给出打印槽中最后的结果,问有哪些按键能确定一定是好的。 思路: 将连续的相同字符取出来,如果长度为奇数,那么一定能确定该字符对应的按键是好的。 代码: view code #pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <bits/stdc++.h> #define fi

【源代码】C++数据结构算法(十五)排序——希尔排序

不想你离开。 提交于 2019-12-02 06:47:26
日常说明:有错误欢迎大家指正。另外本博客所有的代码博主编写后均调试通过。重要提醒!!!!博主使用的是VS2017,如果有低版本的小伙伴最好新建空项目将此代码复制上去。 更多算法请关注我的算法专栏 https://blog.csdn.net/column/details/20417.html 运行结果: ShellSort.h #define LIST_INIT_SIZE 20; #define LIST_CREMENT 10 ; typedef int Datatype; #include<iostream> #include<exception> using namespace std ; class List { public : List(); ~List(); /*void ListInsert(int i, Datatype e);*/ void ListInsert( int n, int values[]); void shell_insert(List&, int increm); void shell_sort(List&); //private: Datatype *elem; int length; int list_size; }; ShellSort.cpp #include "ShellSort.h" #include"malloc.h" List: