c++

COSC346 – C++ Primer Lecture
4
1
C++ Primer
• Almost a superset of C
• Is not fully objectoriented
as procedural
programs are still possible
• Is therefore a multiparadigm
language (both OO
and procedural)
• Review Chapter 4 and 5 of Budd.
COSC346 – C++ Primer Lecture
4
2
Hello World
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << “Hello World” << endl;
}
COSC346 – C++ Primer Lecture
4
3
Hello World 2
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
cout << “Hello World” << endl;
}
COSC346 – C++ Primer Lecture
4
4
Hello Computer
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int num;
cin >> num;
cout << “Hello World ” << num
<< endl;
}
COSC346 – C++ Primer Lecture
4
5
Pointers and References
struct IntArray {
int contents[500];
};
int sum(IntArray a);
int sum(IntArray *a);
int sum(IntArray &a);
int sum(const IntArray &a);
COSC346 – C++ Primer Lecture
4
6
New and Delete
• C++ has both statically allocated and dynamically
allocated memory (like C, unlike Java).
• But you should never have to use malloc/free in a
C++ program.
• Always use new and delete.
• Is not garbage collected unless 3rd party libraries
are used.
COSC346 – C++ Primer Lecture
4
7
New and Delete
int *val;
int *valArray;
val = new int;
valArray = new int[500];
// do stuff with val & valArray
...
delete val;
delete [] valArray;
COSC346 – C++ Primer Lecture
4
8
Classes
• Maintains C style separation of interface and
implementation.
• Class definition in a header file (usually .h)
• Class implementation in a source file
(.cc, .cpp, .cxx).
COSC346 – C++ Primer Lecture
4
9
Class Definition
class Example {
public:
// public interface
protected:
// only accessible to
// subclasses
private:
// only for this class
}
COSC346 – C++ Primer Lecture
4
10
example.h
#ifndef __EXAMPLE_H
#define __EXAMPLE_H 1
class Example {
public:
void method();
...
};
#endif
COSC346 – C++ Primer Lecture
4
11
example.cc
#include “example.h”
void Example::method()
{
cout << “Hello World” << endl;
}
COSC346 – C++ Primer Lecture
4
12
Canonical Class
class Example {
public:
// default constructor
Example();
// copy constructor
Example(const Example &);
// assignment operator
Example &operator=(const Example &);
// destructor
~Example();
private:
int *intArray;
}
COSC346 – C++ Primer Lecture
4
13
Example::Example(){
intArray = new int[500];
}
Example::~Example(){
delete [] intArray;
}
...
int main() {
Example e1;
Example e2=e1;
Example e3;
e3 = e2;
}
COSC346 – C++ Primer Lecture
4
14
Example::Example(const Example &ex) {
intArray = new int[500];
for (int i=0;i<500;i++)
intArray[i]= ex.intArray[i];
}
Example& Example::operator=(const Example &ex){
if (&ex!=this) {
for (int i=0;i<500;i++)
intArray[i]=ex.IntArray[i];
}
}
COSC346 – C++ Primer Lecture
4
15
Canonical Class
• To save yourself future grief, always include the 4
methods in any class definition.
• Even if you don't think you'll ever need them!
• Use g++ Weffc++
to get g++ to warn you.
COSC346 – C++ Primer Lecture
4
16
Operator Overloading
• One of the strengths of C++ compared to Java is
operator overloading (such as =).
• Most operators can be overloaded including
=,+,,/,*,%,^,<,>,<=,>=,!=,...
COSC346 – C++ Primer Lecture
4
17
class Example {
...
Example operator+(const Example &ex) {
Example res;
for (int i=0; i<500; i++)
res.intArray[i] =
intArray[i] + ex.intArray[i];
return res;
}
Operator Overloading
COSC346 – C++ Primer Lecture
4
18
Derived Classes
class Base {
public:
virtual void print(){
cout << “Base class” << endl;
}
}
class Derived:public Base {
public:
virtual void print(){
cout << “Derived class” << endl;
}
}
COSC346 – C++ Primer Lecture
4
19
Derived Classes
void func(Base &b) {
b.print();
}
int main() {
Base b;
Derived d;
func(b);
func(d);
}
COSC346 – C++ Primer Lecture
4
20
Casting
• In Java and C (and C++):
Base *b = new Derived();
Derived *d = (Derived *)b;
• Better C++:
Derived *d = dynamic_cast<Derived*>(b);
if (d) {
// continue doing stuff with d
}
// else can't convert b to a Derived
COSC346 – C++ Primer Lecture
4
21
Generics/Templates
• Type safe containers:
vector<int> list_of_ints;
list_of_ints.push_back(1);
list_of_ints.push_back(2);
list_of_ints.push_back(3);
for (int i=0;i<list_of_ints.size();i++)
// no need for casts!
cout << list_of_ints[i] << endl;
COSC346 – C++ Primer Lecture
4
22
Standard Library
• Relatively small but extremely useful.
• Containers:
– vector, list, deque, queue, stack, map, set, bitset
• I/O
– cout, cin, cerr, ifstream, ofstream, ...
• Strings
• Math stuff
• Algorithms
– sort, binary search, merge, set operations, ...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值