ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++入门基础

2020-12-13 21:30:22  阅读:198  来源: 互联网

标签:function exponent 入门 int 基础 C++ base main cout


C++

C++ characteristics

  • vector
  • list, forward_list(singly-linked list), …
  • map, unordered_map(hash table), …
  • set, multi_set, …
  • string
  • thread
  • memory management

C++

  • compilation + linking
  • OOP
  • safe programming
  • superset of C
  • case sensitive

1 Hello world! print

#include<iostream.h> // header file: input and output stream
int main() // main function, machine's function
{
	cout << "Hello World!\n"; // not c++ output: statement cout << is included in #include<iostream.h>
    						// cout = console out, cout is an object for output to screen, << is operator
    						// "Hello World!\n" is operands
	return 0; // output of machine (main), return 0 means function success, can be other integer
    		  // this line optional
}

2 Variables

#include<iostream.h>
int main() // main function
{
	int a; // declaration
	a = 5; // initialization
	int b = a; // get the value of b
	a = 4; // another valur of a assigned
	cout << a; // output a
	cout << "\n"; // new line
	cout << b; // output b
	cout << "\n";
	return 0;
}

3 Better output

#include<iostream.h>
int main() // main function
{
	int a; // declaration
	a = 5; // initialization
	int b = a; // get the value of b
	a = 4; // another valur of a assigned
	cout << "a = " << a << "\n" << "b = " << b << endl; // output a and b, need only one cout, 
    													//endl = end line = "\n", not like c
    													// don't need %d to represent number
	return 0;
}

4 input

#include<iostream.h>
int main() // main function
{
	int a; // declaration
	cin >> a; // console in, 45 is the input of a 
	cout << "a = " << a << endl; // console out
	return 0;
}

5 functions used in Library

#include<iostream.h>
#include<cmath>
int main() // main function
{
	cout << pow(10, 2) << endl;
	return 0;
}

#include<iostream.h>
#include<cmath>
int main() // main function
{
	int base, exponent;
	cout << "What is the base?: ";
	cin >> base;
	cout << "What is the exponent?: ";
	cin >> exponent;
	cout << pow(base, exponent) << endl;
	return 0;
}

#include<iostream.h>
#include<cmath>
int main() // main function
{
	int base, exponent;
	cout << "What is the base?: ";
	cin >> base;
	cout << "What is the exponent?: ";
	cin >> exponent;
	double power = pow(base, exponent); // if use the value returned in function
	cout << power << endl;
	return 0;
}

6 creating custom functions

#include<iostream.h>

double power(double base, int exponent) // declaring and defining
{
	double result = 1;
	for(int i = 0; i < exponent; i++)
	{
		result = result * base;
	}
	return result;
}

int main() // main function
{
	int base, exponent;
	cout << "What is the base?: ";
	cin >> base;
	cout << "What is the exponent?: ";
	cin >> exponent;
	double my_power = power(base, exponent); // if use the value returned in function
	cout << my_power << endl;
	return 0;
}

标签:function,exponent,入门,int,基础,C++,base,main,cout
来源: https://blog.csdn.net/weixin_43012796/article/details/111145635

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有