Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
krysseltillada / Person.h
Created July 7, 2015 14:56
class Person with constructor(constructor initializer list)
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
#include <string>
struct Person {
/*
Person operator=(const Person &person_p) {
@krysseltillada
krysseltillada / Person.h
Created July 6, 2015 13:01
PERSON_H with operator overloading =
#ifndef PERSON_H /// header declaration --> if not define
#define PERSON_H /// then define PERSON_H as a header
#include <iostream> /// std::end; std::cout;
#include <string> /// std::string;
struct Person {
Person operator=(const Person &person_p) { /// declares a function operator = to mimic a var = var assignment
name = person_p.name; /// the left operand is the "this object" which is p2 and the right operand which we get in the argument is p1
address = person_p.address; /// assigning those values
@krysseltillada
krysseltillada / ncmf_cmf.cpp
Created July 5, 2015 11:04
non_const && const member functions
#include <iostream> /// std::cout; std::endl;
#include <string> /// std::string;
struct race
{
void non_const_display() /// for non const just for to differentiate the two functions to understand
{
std::cout << "class: " << this->Class << std::endl
<< "job: " << this->job << std::endl
<< "level: " << this->level << std::endl
@krysseltillada
krysseltillada / this.cpp
Created July 5, 2015 10:02
(*this).data_member && this->data_member
#include <iostream> /// std::cout; std::endl;
#include <string> /// std::string;
struct person
{
void display()
{
std::cout << "name = " << name << std::endl /// this was initialized and takes the address which this points which is p1 implicitly
<< "this->name = " << this->name << std::endl /// this points to an object which is p1 and dereference it and accessing those member which is name
<< "(*this).name = " << (*this).name << std::endl; /// this deference first what this points which is p1 and accessing those member which is name
#include <iostream> /// std::cout; std::endl; std::cerr;
#include "Sales_data.h" /// Sales_data; o.isbn();
int main ()
{
Sales_data total;
if (std::cin >> total) {
Sales_data trans;
@krysseltillada
krysseltillada / snfvrfp.cpp
Created June 30, 2015 15:24
storing a number of functions in a vector that returns a function pointer and accessing them
#include <iostream>
#include <vector>
int display (int, int);
int sum (int n1, int n2)
{
return n1 + n2;
}
@krysseltillada
krysseltillada / snfv.cpp
Created June 29, 2015 23:33
storing a number of functions in a vector and accessing them
#include <iostream> /// std::cout; std::endl;
#include <vector> /// std::vector;
int display (int, int);
int sum (int n1, int n2)
{
return n1 + n2;
}
@krysseltillada
krysseltillada / fpe.cpp
Created June 29, 2015 15:19
function pointers exercise3
#include <iostream>
/// function pointer
int sum (int i, int ii)
{
return i + ii;
}
int (*ptr_f)(int, int) = sum; /// ptr_f is a function pointer that have a parameters of two int and returns int
@krysseltillada
krysseltillada / fpe.cpp
Created June 28, 2015 17:17
function pointers (exercise)
#include <iostream>
/// function pointers
double sample_func (double d1, double d2)
{
std::cout << __func__ << " was called ";
}
int add (int n1, int n2)
{
@krysseltillada
krysseltillada / fpp.cpp
Created June 28, 2015 10:23
function pointer parameter
#include <iostream> /// std::cout; std::endl;
void useBigger (const std::string &s1, const std::string &s2, /// function useBigger has a parameters of two reference to const string and one pointer type that has paramters pf two const string and returns bool and returns void
bool (*pf)(const std::string &, const std::string &))
{
bool valid = pf (s1, s2); /// equivalent as lengthCompare (s1, s2);
std::cout << __func__ << " is called "
<< valid <<std::endl;
}