75 exmamples to learn C++
# 1. Chapter 1 & 2
# 1.1. C and C++
C++ is a superset of C
#include <iostream>
using namespace std;
int main()
{
cout << "Hi there! This is a C++ world" << endl;
//"<<" means sent to
// enl means "\n"
int someNumber; // hold user input in this variable
cout << "Please enter some number:";
cin >> someNumber; // C translation : scanf("%d",&someNumber); cin means standard input stream; ">>" means get from
cout << "The number you entered was: " << someNumber<< endl;
return 0
}
# 1.1.1. C++ namespace
Is like class and will define the global or local variables
namespace client_code {
int someVariable = 100;
int doSomething() {
}
}
&
namespace utilities {
char someVariable = 'a';
int doSomethingElse() {
}
}
// the someVariables variable in each namespace is local variable.
using namespace std;
int someGlobalVariable // global var here
# 1.1.1.1. How to refer a variable in another diferent namespace
scope The Scope resolution operator
"::"
#include <iostream>
using namespace std;
int a = 10; // global
namespace private_space {
int a = -10;
}
int main(){
cout<< "Hi there" << endl;
int a = 20;
cout<< "Variable a(local) = " << a << enl;// prints 20 . if there is no local then use global variable.
cout<< "Variable a(global) = " << ::a << enl;// prints 10
cout<< "Variable a(private_space) = " << private_space::a << enl;// prints -10
}
# 1.1.2. cout and cin
cout use << operator cin use >> operator
operators can do different things base on the context
The programmer can also redefine a operator
# 1.1.3. C++ function overloading
struct ComplexNumber
{
int realPart
int imageinaryPart;
};
Struct ComplexNumber p1 = {4,0};
Struct ComplexNumber p2 = {3,5};
Struct ComplexNumber p2 = p1 + p2;
// just an example
//check this example
double getArea(double radius)
{
returen 3.14 * radius * radius
}
doubel getArea(double length,double breadth)
{
return length * breadth;
}
But C++ Functions can not be OVERLOADED on the RETURN TYPE!
# 1.1.4. default value for function
pretty relevent to last section overloading. Cause you need to be cautious when setting it.
double print_3D_Coordinates(double x=0, double y=0,double z =0)
{
cout << "A piont in 3-D space: x = "<<x<<",y ="<<",z="<<z<<endl;
}
print_3D_Coordinates();// prints "x=0,y=0,z=0"
print_3D_Coordinates(10);// prints "x=10,y=0,z=0"
print_3D_Coordinates(10,20);// prints "x=10,y=20,z=0"
print_3D_Coordinates(10,20,30);// prints "x=10,y=20,z=30"
# 1.1.5. reference
How to swap two variables value in C++ (if in C you need to use pointer) well, reference acutally is poitner but the syntax is easier
double swap(int& a,int& b) // "int&" means "int reference" or ' a referernt to an int "
{
int temp = a;
a = b;
b = temp
}
# 1.1.5.1. const
const double PI = 3.1416; not use #DEFINE ( C language)
well you can not use last reference if it is a const.
# 1.1.6. bool
C does not have bool C++ have bool contains true and false and 0 means false other number means true
# 2. chapter 3 objects and classes
# 2.1. Class
To C. Classes is like a "struct" with functions in C. Struct does not have functions only variables. and the variables are public
# 2.1.1. constructor and Destructor
constructor A member function to take care of taking these values into member varaibles.And assigning to the corresponding member variabsle
Destructor Amember function to take care of cleanning the object before it ceases to exist
Destuctor always has the same name as the class. preceded by a Tilde (~)
The C++ compiler will automatically call the destructor when an object of the class is about to cease to exist
# 2.1.2. Inherit from other class
Other types can build on the existing class
# 2.1.3. example 1 Defind a really simple C++ class
class ComplexNumber
{
private:
float realPart;
float complexPart;
publc:
ComplexNumber() //constructor have the same name with the class. C++ will automaticly call this constructor when there is no argument inside
{
cout << "No arg-constructor called" << endl;
}
void setMemberVariables(double r,double c)
{
realPart = r;
complexPart = c;
}
float getRealPart()
{
return realPart;
}
float getComplexPart()
{
return complexPart;
}
void print()
{
cout<<"real="<< realPart<< "comlex="<< complexPart;
}
};
# 2.1.4. example 2 instantiate an object of a really simple class
instantiate an object of a really simple class = creating a variable of that class
int main()
{
ComplexNumber c; // will auto call constructor now (if there is no construtor. C++ will create one for you)
cout<<"hello there"<< endl;
c.print();
cout<<endl;
c.setMemberVariables(3.14,5.3);
cout<<endl;
c.print();
cout<<"Okey all done"<<endl;
}
# 2.1.5. example 3 invoke member function of an object.
int main()
{
ComplexNumber c1,c2;
cout << "C1 holds"<< endl;
c1.print();
cout << "C2 holds"<< endl;
c2.print();
cout <<endl;
c1.setMemberVariables(3.14,5.3);
cout <<endl;
c1.print();
cout<<endl;>>
c2.print();
cout <<endl;
cout << "Okey-Dokey!" <<endl;
}
# 2.1.6. example 4 correctly use an initianlisation list in a constructor
initianlisation list
ComplexNumber() : realPart(0.0), complexPart(0.0) // this is initianlisation list
{
cout << "No arg-constructor called" << endl;
}
// The reason to use ini... li.. rather than put that into body is to avoid reassignment.
# 2.1.7. example 5 use a DESTRUCTOR properly to clean up member varaiables
The destructor got called AFTER the last line of code was execute.
#include <iostream>
#include <string>
using namespace std;
class Student
{
private:
char * studentName;
public:
Student(const char* name)
{
cout << "bala" << name>>
studentName = new char[50];
strcpy(studentName,name);
cout <<"Initializaed string to" << studentName << endl;
}
~Student()
{
cout << "Freeing up memory for string"<< studentName <<endl;
delete[] studentName;
}
}
# 2.1.8. example 6 ACCESS MODIFIERS
: Private, Public, Protected
All member variables private always
Provide public setter and getter to access the data in it
This is because others can rely on you. Coz you are overall stable, wont change variables
# 2.2. chapter 4 mutiple files system
Normally will spilt into two files
DECLARATION Header file .H
&
DEFINITION Source file .cpp
Body in .cpp
signature in .h
When inclde in another file , only include .h When compile, only compile cpp
- DECLARATION ComplexNumber.h
class ComplexNumber
{
private:
float realPart;
float complexPart;
public:
ComplexNumber();
ComplexNumber(double c, double r);
ComplexNumber(const ComplexNumber& rhs);
}
- DEFINITION .cpp
:: scope resolution
#include "ComplexNumber.h"
ComplexNumber::ComplexNumber() : realPart(0.0),complexPart(0.0)
{
cout<<""<<endl;
}
ComplexNumber::ComplexNumber(double c, double r) : realPart(0.0),complexPart(0.0)
{
cout<<""<<endl;
}
ComplexNumber::ComplexNumber(const ComplexNumber& rhs) : realPart(rhs.realPart),complexPart(rhs.complexPart)
{
cout<<""<<endl;
}
# UTS DSA notes
# Week1
# 1.1 stack and heap in the memeory
stack is static and will be automaticly cleaned
heap is dynamic and need programmer to clean it manuly
# 1.2 C++ has references
they like pointers but they cant change where they are pointing after initialisition And they are transparently dereferenced
Use & operator to create it.
# week2
# 2.1 Delete
delete is need when we've create something with new
otherwise we have a memory leak
# 2.2 Queue
only have three method:
- enqueue()
- dequeue()
- peek()
Deque is a double ended queue. you can add and remove from both sides.
Priority Queue is a queue but the elements inserted with a priority.
# 2.3 Stack
it is like a queue but its a last in first out data strcuture.
you can add to the top and remove from top
class intStack{
public:
virtual ~intStack(){};
virtual void push() = 0;
virtual int pop() = 0;
virtual int peek() = 0;
}
# Week 4
# Divide and Conquer
- binarySearch
- Mergesort
- Quicksort
^ they all use D&C algorithm.