Pointers 101
#include <iostream>
using std::cout;
using std::endl;
int main (int argc, char * const argv[]) {
long number = 50L;
long* pnumber;
pnumber = &number;
std::cout << endl
<< "The value stored in the variable number is "
<< *pnumber
<< endl
<< endl;
return 0;
}
Prints The value stored in the variable number is 50
Increment Array Using Pointer
// g++ -std=c++14 -c -o constants.o constants.cpp
// g++ -o constants constants.o
#include <iostream>
void bookmarks() {
int pages[2] = {1, 2};
int* bookmark = &pages[0];
std::cout << *bookmark++ << " & " << *bookmark << "\n";
}
int main() {
bookmarks();
return 0;
}
Prints 1 & 2
C++11 and Newer Disallow Some Pointer Notation
// g++ -std=c++14 -c -o badpointers.o badpointers.cpp
// g++ -o badpointers badpointers.o
#include <iostream>
void badpointers() {
char* badCharPtr = "ISO C++11 does not allow conversion from string literal to 'char *'";
const char* goodCharPtr = "This is the proper definition";
*badCharPtr = "error: assigning to 'char' from incompatible type 'const char [2]'";
*goodCharPtr = "error: read-only variable is not assignable";
}
int main() {
badpointers();
return 0;
}
Does not compile due to errors
Constructor
//lightbulb.cpp
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
class Part {
private:
int m_id;
string m_description;
public:
Part(int id, string description) {
m_id = id;
m_description = description;
}
int GetID() const {
return m_id;
}
string GetDescription() const {
return m_description;
}
~Part() { }
};
int main() {
Part part(1, "light bulb");
cout << "id:" << part.GetID()
<< "\ndescr:" << part.GetDescription();
}
Prints id:1
Parameter Passing
// refandval.cpp
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
int passByVal(int, int);
int passByRef(int*, int*);
void changedParms(int*, int*);
void unchangedParms(int, int);
int main (int argc, const char * argv[]) {
int x=0, y=7, z=8;
int *yPtr = &y;
int *zPtr = &z;
x = passByVal(4, 5);
cout << "the answer is: " << x << endl;
x = passByRef(yPtr, zPtr);
cout << "the answer is: " << x << endl;
unchangedParms(y, z);
cout << "the answers are: " << y << " and " << z << endl;
changedParms(yPtr, zPtr);
cout << "the answers are: " << y << " and " << z << endl;
return 0;
}
int passByVal(int a, int b) {
return a + b;
}
int passByRef(int *a, int *b) {
return *a + *b;
}
void unchangedParms(int a, int b) {
cout << "in unchangedParms: " << ++a << " and " << ++b << endl;
}
void changedParms(int *a, int *b) {
cout << "in changedParms: " << ++*a << " and " << ++*b < endl;
}
Printsthe answer is: 9
the answer is: 15
in unchangedParms: 8 and 9
the answers are: 7 and 8
in changedParms: 8 and 9
the answers are: 8 and 9
Inheritance, Abstract, Virtual, etc
Shape is an abstract class that has what in Java would be called the "abstract method" GetArea. In Robert C Martin's 1995 C++ book GetArea is described as a pure interface. Square, Rectangle and Triangle extend Shape and override the virtual function to suit its own needs.Note the use of the keyword 'virtual' in the class Shape. All methods in Java are virtual but, in C++, if a method is to be overridden, it must be declared 'virtual'.
//classy.cpp
#include <iostream>
using std::cout;
using std::endl;
class Shape {
public:
virtual double GetArea() = 0;
};
class Square : Shape {
private:
int side;
public:
Square(int i) {
side = i;
}
double GetArea() {
return side * side;
}
};
class Rectangle : Shape {
private:
int length, width;
public:
Rectangle(int l, int w) {
length = l;
width = w;
}
double GetArea() {
return length * width;
}
};
class Triangle : Shape {
private:
int length, width;
public:
Triangle(int l, int w) {
length = l;
width = w;
}
double GetArea() {
return length * width / 2;
}
};
int main (int argc, char * const argv[]) {
Square square(5);
Rectangle rectangle(6,3);
Triangle triangle(6,3);
std::cout << endl << "Square: " << square.GetArea()
<< endl << "Rectangle: " << rectangle.GetArea()
<< endl << "Triangle: " << triangle.GetArea() << endl;
return 0;
};
PrintsSquare: 25
Rectangle: 18
Triangle: 9
Classic C++ Example with Class, enum and Predicate
This is more of a syntax lesson.person.h
#ifndef PERSON_H
#define PERSON_H
class person_t {
public:
enum gender_t {
female,
male,
other
};
enum output_format_t {
name_only,
full_name
};
person_t()
: m_name("John")
, m_surname("Doe")
, m_gender(other)
{
}
person_t(string name, gender_t gender, int age = 0)
: m_name(name)
, m_surname("Doe")
, m_gender(gender)
, m_age(age)
{
}
person_t(string name, const string &surname, gender_t gender, int age = 0)
: m_name(name)
, m_surname(surname)
, m_gender(gender)
, m_age(age)
{
}
string name() const
{
return m_name;
}
string surname() const
{
return m_surname;
}
gender_t gender() const
{
return m_gender;
}
int age() const
{
return m_age;
}
void print(ostream &out,
person_t::output_format_t format) const
{
if (format == person_t::name_only) {
out << name() << '\n';
} else if (format == person_t::full_name) {
out << name() << ' '
<< surname() << '\n';
}
}
private:
string m_name;
string m_surname;
gender_t m_gender;
int m_age;
};
#endif // PERSON_H
person.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include "person.h"
using namespace std;
class older_than {
public:
older_than(int limit)
: m_limit(limit)
{
}
template
bool operator() (T &&object) const
{
return forward(object).age() > m_limit;
}
private:
int m_limit;
};
int main(int argc, char *argv[])
{
vector persons;
vector::iterator it;
it = persons.begin();
person_t p1 = person_t("Bill", person_t::gender_t::male, 21);
person_t p2 = person_t("Samantha", person_t::gender_t::female, 23);
person_t p3 = person_t("Agatha", person_t::gender_t::female, 83);
it = persons.insert(it, p1);
it = persons.insert(it, p2);
it = persons.insert(it, p3);
older_than predicate(42);
int num_items = count_if(persons.begin(), persons.end(), predicate);
cout << num_items << "\n";
return 0;
}
compile, link and run
$ g++ -std=c++14 -c -o person.o person.cpp $ g++ -o person person.o $ ./person 1