struct node { double val; node* next; node* prev; }; struct List { node* front; node* back; }; |
class StockItem { private: std::string description; int numInStock; float price; long itemId; public: StockItem(std::string d="", int n=0, float p=0, long i=0) { description = d; numInStock = n; price = p; itemId = i; } void printItem(); }; |
class tree { private: struct node { int value; node* left; node* right; } *root; void deallocate(node* subtree); int count(node* subtree, int v); void print(node* subtree); void insert(node* &subtree, int v); public: tree() { root = NULL; } ~tree() { deallocate(root); } int count(int v) { return count(root); } void print() { print(root); } void insert(int v) { insert(root, v); } }; |
#include <iostream> using namespace std; class parent1 { public: virtual void print() = 0; }; class parent2 { public: void print() { cout << "print version 1" << endl; } }; class child1: public parent1 { public: child1() { cout << "start child type 1" << endl; } ~child1() { cout << "end child type 1" << endl; } void print() { cout << "print version 2" << endl; } }; class child2: public parent1 { public: child2() { cout << "start child type 2" << endl; } ~child2() { cout << "end child type 2" << endl; } void print() { cout << "print version 3" << endl; } }; class child3: public parent2 { public: child3() { cout << "start child type 3" << endl; } ~child3() { cout << "end child type 3" << endl; } void print() { cout << "print version 4" << endl; } }; int main() { parent1 *p1 = new child1; parent1 *p2; child3 c3; p1->print(); p2 = new child2; p2->print(); delete p2; c3.print(); }
class Circle { private: float x, y, radius; public: Circle(): x(0), y(0), radius(1) { } void print(); void setcoords(float newx, float newy); void setrad(float newrad); }; |
#include <iostream> #include <exception> #include <cmath> class myExc: public std::exception { protected: std::string message; public: myExc(std::string msg) { message = msg; } ~myExc() { } // says what kind of exception it was void what() { std::cout << message << std::endl; } }; void f(double val) { try { if (val < 0) { throw myexc("Negative number"); } std::cout << sqrt(val) << std::endl; } catch (std::exception e) { std::cout << "exception happened" << std::endl; } } int main() { try { double userData; cin >> userData; f(userData); std::cout << "Bye!" << std::endl; } catch (myExc& e) { e.what(); } } |