21 Pages

Lecture-07-Classes-I

Course: CS 07, Fall 2009
School: BYU
Rating:
 
 
 
 
 

Word Count: 606

Document Preview

Classes C++ (I) User-Defined Data Types Some programs can be written using only the built-in C++ data types Most programs create new data types that more effectively model the problem being solved Structs, enums, typedefs can be used to define new data types Classes can be used to create new data types that are fully integrated into the C++ language User-Defined Data Types // BigInteger supports...

Register Now

Unformatted Document Excerpt

Coursehero >> Utah >> BYU >> CS 07

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
Classes C++ (I) User-Defined Data Types Some programs can be written using only the built-in C++ data types Most programs create new data types that more effectively model the problem being solved Structs, enums, typedefs can be used to define new data types Classes can be used to create new data types that are fully integrated into the C++ language User-Defined Data Types // BigInteger supports arbitrary-precision integers class BigInteger { }; int main() { BigInteger x("123451234512345"); BigInteger y(789L); BigInteger z = -((x + y)/2); if (z < x) { ++z; } cout << z << endl; } Data Type Operations Before learning how to create new types with classes, let's look at how the built-in types are used Declare with no initial value float f; Initialize with value of the same type float f = 123.45f; Initialize with value of a different type float f = 789; Assign to value of the same type f = 123.45f; Data Type Operations Assign to value of a different type f = 789; Convert to a different type double d = f; Apply operators (f * g) Print to output stream cout << f ; Deinitialize Goes out of scope Deallocated with delete Defining a new class involves specifying how all of these operations work on objects of the new class String Class Example Let's define a class named String that is similar to the standard C++ string class class String { }; void main() { String byu("BYU"); String gatech; gatech.Append("Georgia Tech"); String msg = gatech.Concat(" threw an interception."); msg.Write(cout); msg = byu.Concat(" kicked a field goal."); msg.Write(cout); for (int x=0; x < msg.Length(); ++x) { cout << msg.GetChar(x); } } class String { private: char * str; }; Instance Variables class String { Constructors public: String() { Init(""); } String(const char * s) { Init(s); } String(const String & s) { Init(s.str); } private: void Init(const char * s) { str = new char[strlen(s) + 1]; strcpy(str, s); } }; class String { public: ~String() { Free(); } private: void Free() { delete [] str; str = 0; } }; Destructor class String { Assignment Operators public: String & operator =(const char * s) { Free(); Init(s); return *this; } String & operator =(const String & s) { if (&s this) != { Free(); Init(s.str); } return *this; } }; class String { Instance Operations public: bool IsEmpty() const { return (Length() == 0); } const char * CString() const { return str; } int Length() const { return strlen(str); } }; class String { Instance Operations public: char GetChar(int index) const { return str[index]; } void SetChar(int index, char c) { str[index] = c; } }; class String { Instance Operations public: void Append(const char * s) { char * newStr = new char[strlen(str) + strlen(s) + 1]; strcpy(newStr, str); strcat(newStr, s); Free(); str = newStr; } void Append(const String & s) { Append(s.str); } }; class String { Instance Operations public: String Concat(const char * s) const { String result(str); result.Append(s); return result; } String Concat(const String & s) const { return Concat(s.str); } }; class String { Instance Operations public: void Write(ostream & s) const { s << str; } }; Tracking the Number of String Objects String * UseStrings() { String a[5]; String b("a string"); cout << String::ObjectCount() << endl; String * c = new String("another string"); cout << String::ObjectCount() << endl; return c; } void main() { cout << String::ObjectCount() << endl; String * p = UseStrings(); cout << String::ObjectCount() << endl; delete ...

Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

BYU - CS - 240
C+ Classes (I)User-Defined Data TypesSome programs can be written using only the built-in C+ data types Most programs create new data types that more effectively model the problem being solved Structs, enums, typedefs can be used to define new dat
BYU - CS - 16
Error Handling &amp; Defensive ProgrammingError Handling ConceptsMurphy's Law &quot;Anything that can go wrong will go wrong&quot; Error conditions will occur, and your code needs to deal with them Out of memory, disk full, file missing, file corrupted, network
BYU - CS - 240
Error Handling &amp; Defensive ProgrammingError Handling ConceptsMurphy's Law &quot;Anything that can go wrong will go wrong&quot; Error conditions will occur, and your code needs to deal with them Out of memory, disk full, file missing, file corrupted, network
BYU - DAM - 83
DataConvert Biological Data File ConversionHelp ManualDeveloped by: Matthew Dyer1, Joshua Sailsbery 1, David McClellan11Department of Integrative Biology, Brigham Young University, Provo, Utah, USAAbstract Summary: The software program DataC
BYU - MFW - 2
BYU - MFW - 2
BYU - MFW - 2
BYU - MFW - 2
BYU - MFW - 2
BYU - MFW - 2
Figure 23Figure 24Figure 25
BYU - MFW - 2
BYU - MFW - 2
BYU - MFW - 2
BYU - MFW - 2
BYU - MFW - 2
CURRICULUM VITAE Boris R. Krasnov Ph.D. in Biology Personal details: Born 19.10.1955, Moscow, USSR (Russia) Married + 2 Immigration to Israel 07.12.1990 Citizenship: Israeli Current address: Ein Mashack str. 14/3, Mizpe-Ramon 80600, Israel. Tel. 972-
BYU - WWWCM - 99
Call for Attendance: International Workshop on the World-Wide Web and Conceptual Modeling WWWCM'99 November 15 - 18, 1999 http:/www.cm99.byu.eduto be held in conjunction with the18th International Conference on Conceptual Modeling (ER'99), Paris,
BYU - WORKSHOP - 01
CALL FOR PAPERSWorkshop on Technology for Family History and Genealogical ResearchMarch 29, 2001 Brigham Young University, Provo Utah, USA www.FamilyHistoryTechnology.byu.edu/workshopSCOPE OF THE WORKSHOP The Workshop on Technology for Family His
BYU - WORKSHOP - 02
xv v} } v t 9txfyig5TyfTyfyawy xv ~ xv } t ~v } xv z x t ~ ~ } } { x 9f 9 fy0yyTfyyTwvfTfGywvfwg0Ta~f$yTf g$RfVH 9 f $fdfH a ~v } } t5Tfaf(y%Ta~f{ t } v ~ t}~ x xv v x t x xv t x t t }
BYU - WORKSHOP - 02
Challenges in Constructing a Digital Microfilm LibraryDan R. Olsen Jr. Brigham Young University Computer Science Department olsen@cs.byu.edu There are approximately 2 million rolls of microfilm in the LDS Family History Library. This is a vast repos
BYU - WORKSHOP - 01
An integrated system for processing information from genealogical textMerrill Hutchison, Tim Richards, William Taysom, and Deryle LonsdaleThis presentation introduces an integrated software system that has been designed and implemented to allow pr
BYU - CH - 4
Problem 4.31 Find the electric potential V at a location a distance b from the origin in the xy plane due to a line charge with charge density l and of length l. The line charge is coincident with the z-axis and extends from z = l=2 to z = l=2.z l/2
BYU - CH - 8
Problem 8.3 A plane wave traveling in a medium with r1 = 9 is normally incident upon a second medium with r2 = 4. Both media are made of nonmagnetic, nonconducting materials. If the magnetic eld of the incident plane wave is given by Hi = z 2 cos(2
BYU - CH - 1
Problem 1.6A wave traveling along a string in the y1 (x; t) = A cos(t+x-direction isgiven byx);where x = 0 is the end of the string, which is tied rigidly to a wall, as shown in Fig. 1-21 (P1.6). When wave y1 (x; t) arrives at the wall, a r
BYU - CH - 8
Problem 8.30 A perpendicularly polarized wave in air is obliquely incident upon a planar glass-air interface at an incidence angle of 30 . The wave frequency is 600 THz (1 THz = 1012 Hz), which corresponds to green light, and the index of refraction
BYU - CH - 8
Problem 8.14 Consider a thin lm of soap in air under illumination by yellow light with = 0:6 m in vacuum. If the lm is treated as a planar dielectric slab with r = 1:72, surrounded on both sides by air, what lm thickness would produce strong reectio
BYU - CH - 2
Problem 2.35 Use the Smith chart to nd the reection coefcient corresponding to a load impedance: (b) ZL 2 2 j Z0 . Solution: Refer to Fig. P2.35.0.110.1 0.40.12 0.380.13 0.370.14 0.36800.1 0.3 5 51.00.71.6 0.0.42608 .00
BYU - CH - 3
Problem 3.45 by evaluating: Z (a) (b)S Verify Stokess theorem for the vector eld B = (r cos + sin ) r over the semicircular contour shown in Fig. P3.45(a), and over the surface of the semicircle.y 2 yZCnB dl( B) dsL22 1L3 L41