CS 301 Algorithms & Complexity Inclass Code 20200115

Arrays and pointer syntax; command-line parameters

ConsoleApplication1.cpp

// ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include <iostream>
#include "Pokemon.h"

using namespace std;

void alex1(Pokemon p) {
	p.printStatus();
}

void alex2(Pokemon* p) {
	p->printStatus();
}

int main()
{
	int litian = 5;
	int* litianAddr = &litian;
	cout << "Hello World! Litian's value is " << litian << "\n";
	cout << "and Litian's address is " << litianAddr << endl;
	Pokemon p1;
	p1.printStatus();
	Pokemon p2("Mewtwo", 100);
	p2.printStatus();
	p2.setHP(40);

	//initialize an object with its memory address, using pointer syntax  
	Pokemon* p3 = new Pokemon();
	Pokemon* p4 = new Pokemon("Mew", 40);
	//how to call method on pointer object? Not with ".", but using "->" 
	p3->printStatus();
	p3->setHP(80);
	p4->printStatus();

	//calling refference is faster, calling by value copys an object, then dothing on it, that’s slower.  
	for (int i = 0; i < 1000; i++) {
		alex1(p2);//slower
	}

	for (int i = 0; i < 1000; i++) {
		alex2(p4);//faster
	}

	int rex[10]; //this syntax requires a literal number for the array size
	rex[0] = 42;
	cout << "The first value in array rex is " << rex[0] << endl;

	//the size of the array has to be known  
	//if you want to set your array dynamically, you have to use pointer syntax.  
	int n = 10;
	int* ivan = new int[n];
	ivan[0] = 45;
	ivan[1] = 90;
	ivan[2] = 11;
	ivan[2] = 87;

	//C++ arrays don't know their own size.
	//you have to tell the lenth somewhere else. like n is define before 
	for (int i = 0; i < n; i++) {
		//do something
	}


}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

Pokemon.cpp

#include "pch.h"
#include "Pokemon.h"
#include <iostream>

using namespace std;

Pokemon::Pokemon()
{
	name = "Detective Pikachu";
	hp = 10;
}

Pokemon::Pokemon(string n, int h) {
	name = n;
	hp = h;
}

void Pokemon::printStatus() {
	cout << "My name is " << name << endl;
	cout << "My HP is " << hp << endl;
}

void Pokemon::setHP(int h) {
	hp = h;
}

Pokemon::~Pokemon()
{
	//nothing here
}

Pokemon.h

#pragma once

#include <string>

using namespace std;

class Pokemon
{
public:
	Pokemon();
	Pokemon(string, int);
	~Pokemon();
	void printStatus();
	void setHP(int);

private:
	int hp;
	string name;
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值