CS302 Topic: More on C+ +
Tuesday, Sept. 13, 2005
- r, “Your life as a CS302 student…”
CS302 Topic: More on C+ + or, Your life as a CS302 student - - PowerPoint PPT Presentation
CS302 Topic: More on C+ + or, Your life as a CS302 student Tuesday, Sept. 13, 2005 Announcements Lab 2 (Golf Handicaps) is due this Friday, Sept. 16!! Lab 3 (Stock Charts) will be made available soon It will be due
class Stack { int topStack; Stack stack; // illegal--class definition not yet complete };
class Stack; // forward declaration class Stack { int topStack; Stack *stack; // legal: forward class def’n. has been seen };
class tree_node { tree_node *left_child; // legal: class declared when name seen tree_node *right_child; };
class Dlist; class Dlnode { friend class Dlist; protected: ... };
class myClass { int a, b; //default to private public: myClass(int I, int j) {a=I; b=j;} friend int comDenom(myClass x); }; int comDenom(myClass x) { //Note that since comDenom is a friend function //of myClass, it can directly access a and b int max = x.a < x.b ? x.a : x.b; for (int i=2; i <= max; i++) if ((x.a%i) == 0 && (x.b%i) == 0) return i; return 0; }
int getSize() { return size; } int getSize() { return this->size; }
inline int f() { … }
class cl { int i; public: int get_i() {return i;} void put_i(int j) { i = j; } };
using namespace std; // C++ String class
length, c_str, =, +=, ==, !=, <, <=, >, >=, [ ]
#include <stdio.h> #include <iostream> #include <string> using namespace std; main() { string str1, str2; const char *s2; char *s3; str1 = "Hello World"; printf("%d %s\n", str1.length(), str1.c_str()); cout << str1.length() << " " << str1 << "\n"; str2 = str1; cout << str2 << "\n"; str1[0] = 'J'; cout << str1 << "\n" << str2 << "\n"; s2 = str2.c_str(); cout << s2 << " " << str2 << "\n"; s3 = "Daffy Duck"; str2 = s3; str2[0] = 'T'; cout << s3 << " " << str2 << "\n"; str2 += " "; str2 += str1; cout << str2 << "\n"; }
#include <stdio.h> #include <iostream> #include <string> using namespace std; main() { string str1, str2; const char *s2; char *s3; str1 = "Hello World"; printf("%d %s\n", str1.length(), str1.c_str()); cout << str1.length() << " " << str1 << "\n"; str2 = str1; cout << str2 << "\n"; str1[0] = 'J'; cout << str1 << "\n" << str2 << "\n"; s2 = str2.c_str(); cout << s2 << " " << str2 << "\n"; s3 = "Daffy Duck"; str2 = s3; str2[0] = 'T'; cout << s3 << " " << str2 << "\n"; str2 += " "; str2 += str1; cout << str2 << "\n"; }
#include <stdio.h> #include <iostream> #include <string> using namespace std; main() { string str1, str2; const char *s2; char *s3; str1 = "Hello World"; printf("%d %s\n", str1.length(), str1.c_str()); cout << str1.length() << " " << str1 << "\n"; str2 = str1; cout << str2 << "\n"; str1[0] = 'J'; cout << str1 << "\n" << str2 << "\n"; s2 = str2.c_str(); cout << s2 << " " << str2 << "\n"; s3 = "Daffy Duck"; str2 = s3; str2[0] = 'T'; cout << s3 << " " << str2 << "\n"; str2 += " "; str2 += str1; cout << str2 << "\n"; }
11 Hello World 11 Hello World Hello World Jello World Hello World Hello World Hello World Daffy Duck Taffy Duck Taffy Duck Jello World
#include <stdio.h> #include <iostream.h> #include <string> using namespace std; class Bert { public: Bert(char *); ~Bert(); void add_bert(); string getString(); int getLength(); protected: string str; } Bert::Bert(char *s) { printf(“Creating new Bert (%s)\n”, s); str = s; } Bert::~Bert() { printf(“Bert destructor called (%s)\n”, str.c_str()); } void Bert::add_bert() { str += “bert”; } string Bert::getString() { return str; } int Bert::getLength() { return str.length();}
#include <stdio.h> #include "bert.h" main() { Bert s1("Dog"), s2("Rat"); s1.add_bert(); s2.add_bert(); printf("S1: %s\n", s1.getString().c_str()); }
Creating new Bert (Dog) Creating new Bert (Rat) S1: Dogbert Bert destructor called (Ratbert) Bert destructor called (Dogbert)
#include <stdio.h> #include <iostream.h> #include <string> using namespace std; class Bert { public: Bert(char *); ~Bert(); void add_bert(); string getString(); int getLength(); protected: string str; } Bert::Bert(char *s) { printf(“Creating new Bert (%s)\n”, s); str = s; } Bert::~Bert() { printf(“Bert destructor called (%s)\n”, str.c_str()); } void Bert::add_bert() { str += “bert”; } string Bert::getString() { return str; } int Bert::getLength() { return str.length();}
#include <stdio.h> #include "bert.h" void a() { Bert s2("Rat"); s2.add_bert(); printf("S2: %s\n", s2.getString().c_str()); } int main() { Bert s1("Dog"); a(); printf("S1: %s\n", s1.getString().c_str()); a(); return 0; }
Creating new Bert (Dog) Creating new Bert (Rat) S2: Ratbert Bert destructor called (Ratbert) S1: Dog Creating new Bert (Rat) S2: Ratbert Bert destructor called (Ratbert) Bert destructor called (Dog)
#include <stdio.h> #include <iostream.h> #include <string> using namespace std; class Bert { public: Bert(char *); ~Bert(); void add_bert(); string getString(); int getLength(); protected: string str; } Bert::Bert(char *s) { printf(“Creating new Bert (%s)\n”, s); str = s; } Bert::~Bert() { printf(“Bert destructor called (%s)\n”, str.c_str()); } void Bert::add_bert() { str += “bert”; } string Bert::getString() { return str; } int Bert::getLength() { return str.length();}
#include <stdio.h> #include "bert.h" void a(Bert s2) { s2.add_bert(); printf("S2: %s\n", s2.getString().c_str()); } main() { Bert s1("Dog"); a(s1); printf("S1: %s\n", s1.getString().c_str()); a(s1); }
Creating new Bert (Dog) S2: Dogbert Bert destructor called (Dogbert) S1: Dog S2: Dogbert Bert destructor called (Dogbert) Bert destructor called (Dog)
#include <stdio.h> #include <iostream.h> #include <string> using namespace std; class Bert { public: Bert(char *); ~Bert(); void add_bert(); string getString(); int getLength(); protected: string str; } Bert::Bert(char *s) { printf(“Creating new Bert (%s)\n”, s); str = s; } Bert::~Bert() { printf(“Bert destructor called (%s)\n”, str.c_str()); } void Bert::add_bert() { str += “bert”; } string Bert::getString() { return str; } int Bert::getLength() { return str.length();} #include <stdio.h> #include "bert.h" Bert Global_bert("Rat"); main() { Bert s1("Dog"); s1.add_bert(); printf("S1: %s\n", s1.getString().c_str()); printf("GJ: %s\n", Global_bert.getString().c_str()); }
Creating new Bert (Rat) Creating new Bert (Dog) S1: Dogbert GJ: Rat Bert destructor called (Dogbert) Bert destructor called (Rat)
#include <stdio.h> #include <iostream.h> #include <string> using namespace std; class Bert { public: Bert(char *); ~Bert(); void add_bert(); string getString(); int getLength(); protected: string str; } Bert::Bert(char *s) { printf(“Creating new Bert (%s)\n”, s); str = s; } Bert::~Bert() { printf(“Bert destructor called (%s)\n”, str.c_str()); } void Bert::add_bert() { str += “bert”; } string Bert::getString() { return str; } int Bert::getLength() { return str.length();} #include <stdio.h> #include "bert.h" Bert Global_bert("Rat"); main() { Bert s1("Dog"); s1.add_bert(); printf("S1: %s\n", s1.getString().c_str()); printf("GJ: %s\n", Global_bert.getString().c_str()); exit(0); }
Creating new Bert (Rat) Creating new Bert (Dog) S1: Dogbert GJ: Rat Bert destructor called (Rat)
#include <stdio.h> #include <iostream.h> #include <string> using namespace std; class Bert { public: Bert(char *); ~Bert(); void add_bert(); string getString(); int getLength(); protected: string str; } Bert::Bert(char *s) { printf(“Creating new Bert (%s)\n”, s); str = s; } Bert::~Bert() { printf(“Bert destructor called (%s)\n”, str.c_str()); } void Bert::add_bert() { str += “bert”; } string Bert::getString() { return str; } int Bert::getLength() { return str.length();} #include <stdio.h> #include "bert.h" Bert a() { Bert s2("Rat"); s2.add_bert(); printf("S2: %s\n", s2.getString().c_str()); return s2; } main() { Bert s1("Dog"); printf("S1: %s\n", s1.getString().c_str()); s1 = a(); printf("S1: %s\n", s1.getString().c_str()); }
Creating new Bert (Dog) S1: Dog Creating new Bert (Rat) S2: Ratbert Bert destructor called (Ratbert) S1: Ratbert Bert destructor called (Ratbert)
#include <stdio.h> #include <iostream.h> #include <string> using namespace std; class Bert { public: Bert(char *); ~Bert(); void add_bert(); string getString(); int getLength(); protected: string str; } Bert::Bert(char *s) { printf(“Creating new Bert (%s)\n”, s); str = s; } Bert::~Bert() { printf(“Bert destructor called (%s)\n”, str.c_str()); } void Bert::add_bert() { str += “bert”; } string Bert::getString() { return str; } int Bert::getLength() { return str.length();}
#include <stdio.h> #include "bert.h" main() { Bert *s1; s1 = new Bert("Dog"); s1->add_bert(); printf("S1: %s\n", s1->getString().c_str()); s1 = new Bert("Rat"); s1->add_bert(); printf("S1: %s\n", s1->getString().c_str()); }
Creating new Bert (Dog) S1: Dogbert Creating new Bert (Rat) S2: Ratbert