Typedef Int Class Public Static Const Int Private Int Public Void Void Bool
Typedef Int Class Public Static Const Int Private Int Public Void Void Bool
public: void push(Item val); void pop(); Item top(); bool isempty(); Stack(); };
#include <iostream> #include "Stack.h" using namespace std; Stack::Stack() { nvalid=0; } void Stack::push(Item val) { arr[nvalid]=val; nvalid++; } void Stack::pop() { if(isempty()==1) cout << "Stack is empty" <<endl; else nvalid--; } bool Stack::isempty() { if(nvalid==0) return true; else return false; } Item Stack::top() { if(isempty()==1) { cout << "Stack is empty" <<endl; return 0; } else { int p=nvalid-1; return arr[p]; } }
#include <iostream> // Needed to refrence ostream and istream using namespace std; void print(ostream& os) const; void read(istream& is); friend ostream& operator<<(ostream& os, const Fraction& f); // Overload insertion operator friend istream& operator>>(istream& is, Fraction& f); // Overload extraction operator Fraction operator+(const Fraction rhs); // Overload addition operator istream& operator>>(istream& is, Fraction& f) { f.read(is); // calls read function return is; // returns is of type istream } void Fraction::read(istream& is) { // user enters fraction (no prompts needed) is>>num; // cin replaced with is char slash; is>>slash; // slash seperates two integer values is>>den; while(den==0) // if den==0 keep waiting for useable den value is>>den; } ostream& operator<<(ostream& os, const Fraction& f) { f.print(os); // calls print function return os; // returns os of type ostream }
void Fraction::print(ostream& os) const { // simply prints fraction os << num // cout replaced with os << "/" << den; } Fraction Fraction::operator+(const Fraction rhs) { Fraction h(num, den); // creates Fraction h from num and den return h.add(rhs); // returns sum of h and rhs } Fraction Fraction::add(Fraction f) const { // adds two fractions and returns a fraction int n, d; // creats two variables to store new fractions num and den n=(f.num)*(den)+(num)*(f.den); d=(f.den)*(den); return Fraction(n, d); }
#include <iostream> using namespace std; int main() { char* cptr; char c1='A'; cptr=&c1; cout << *cptr << endl; // prints A // Can now change c1 OR *cptr *cptr='B'; cout << *cptr << endl; // prints B char* point=cptr; cout << *point << endl; // B int score[8]; int* point2; point2=score; point2=&score[0]; // both the same // score=point2; FAILS point2[0]=6; *point2=6; //same cout << *point2 << endl; // 6 char str1[]="Hello"; // str2=strl; FAIL constant pointers char* str; str=str1; cout << str << endl; // Hello *str='j'; cout << str << endl; // jello // Array of Strings char* words[128]; char m1[3]="hi"; words[0]=m1; char input[64] words[1]=input; cin >> words[1]; }
Just in case
const char* substr(const char* cstr1, const char* cstr2); int main() { char* a= "facebook"; char* b= "book"; const char* g=substr(a,b); if(g==NULL) return 0; cout << g; cin.get(); return 0; }