90 likes | 186 Views
Ch6 & 12 Note. Dr. Wang. Strings for C, C++, Java. C++ string – string str = “VWC”; C- string – char cstr [21] = {‘V’, ‘W’, ‘C’, ‘’}; char cstr2[21] = “VWC”; Assignment is valid for C++ string, not C-string: str = “ODU”; // valid cstr2 = “ODU”; // invalid
E N D
Ch6 & 12 Note Dr. Wang
Strings for C, C++, Java C++ string – string str = “VWC”; C- string – char cstr[21] = {‘V’, ‘W’, ‘C’, ‘\0’}; char cstr2[21] = “VWC”; Assignment is valid for C++ string, not C-string: str = “ODU”; // valid cstr2 = “ODU”; // invalid strcpy (cstr2, “ODU”); // valid • I/O similar: cin >> str; // “CS202” cin >> cstr; // “CS380”
Strings for C, C++, Java (ctd) Java string – String st1, st2; …. st1 = “VWC”; str2 = str1; • I/O total different from C/C++ System.out.println(str1); Input uses the Scanner class – Scanner in = new Scanner(System.in); str = in.next(); // a single string
Enumeration types, p.259 • C/C++ enum Gender {MALE, FEMALE}; Gender t1 = MALE; Gender t2 = FEMALE; • Java interface for enumeration
Arrays, p.264 • C/C++: arrays have a limitation for storage (static) const int MAX = 100; int num[MAX] = {1, 2, 3}; … • Java array is dynamic int x; … intarr[] = new int[x];
Record Types, p.282 • C/C++ struct • Java class
Pointers & References, p.291 • C/C++ int* ptr = new int; *ptr = 7; int x = 12; int* qtr = &x; *qtr = *ptr; cout << *ptr << “\t” << *qtr << “\t” << x;
Ch 12 OOP • Procedure-oriented P. L. – C, Fortran, .. • Object-O. P.L. – C++, Java, Clips, Alice • Inheritance, composition, & polymorphism • C++: data members, function members • Java: instance variables, methods
Ch 12 OOP Inheritance • Super class sub class • A subclass may inherit all methods and instance variables (in C++: functions, data)