0% found this document useful (0 votes)
61 views3 pages

Typedef Int Class Public Static Const Int Private Int Public Void Void Bool

The document defines a Stack class with methods to push, pop, check if empty, and access the top element of the stack. It also defines a Fraction class with overloaded operators for addition and input/output streaming. Main tests using pointers with built-in arrays and strings.

Uploaded by

Vanillasky06
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views3 pages

Typedef Int Class Public Static Const Int Private Int Public Void Void Bool

The document defines a Stack class with methods to push, pop, check if empty, and access the top element of the stack. It also defines a Fraction class with overloaded operators for addition and input/output streaming. Main tests using pointers with built-in arrays and strings.

Uploaded by

Vanillasky06
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

typedef int Item; class Stack { public: static const int MAX=100; private: Item arr[MAX]; int nvalid;

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; }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy