classes

classes - 5/13/10 1 Composite Types: Classes Similar to...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 5/13/10 1 Composite Types: Classes Similar to structs only with member functions as well as member data Storage classes are the same Object is an instance of a class Held by an automatic/static class variable (a non-pointer type) Allocated automatically on the stack if local, with the data segment if global Members (data/functions) accessed via the dot (.) operator (obj.field = 2;) Initialized using a constructor from the class A member function that initializes a newly allocated object Does not return a value; Called indirectly at program start or when a function is invoked MyClass obj; //note NO parenthesis! Calls default constructor: takes no parameters: MyClass(); Can take arguments: MyClass(int,int,int); MyClass obj(i,j,k); 1 Composite Types: Classes Similar to structs only with member functions as well as member data Storage classes are the same Object is an instance of a class Held by an automatic/static class variable (a non-pointer type) Allocated automatically on the stack if local, with the data segment if global Members (data/functions) accessed via the dot (.) operator (obj.field = 2;) Initialized using a constructor from the class A member function that initializes a newly allocated object Does not return a value; Called indirectly at program start or when a function is invoked MyClass obj; //note NO parenthesis! Calls default constructor: takes no parameters: MyClass(); If there is one argument/parameter and its type is an object of that same class, its called the copy constructor typically: MyClass(const MyClass&); MyClass obj2(obj1); 2 Composite Types: Classes Similar to structs only with member functions as well as member data Storage classes are the same Object is an instance of a class Held by an automatic/static class variable (a non-pointer type) Allocated automatically on the stack if local, with the data segment if global Members (data/functions) accessed via the dot (.) operator (obj.field = 2;) Initialized using a constructor from the class A member function that initializes a newly allocated object Does not return a value; Called indirectly at program start or when a function is invoked MyClass obj; //note NO parenthesis! Calls default constructor: takes no parameters: MyClass(); When a function returns, the destructor is called on all local/auto objects When a program ends, the destructor is called on all global/static objects There is only one destructor, it takes not parameters and is called indirectly. Its prototype is: ~MyClass(); 3 Composite Types: Classes Similar to structs only with member functions as well as member data Storage classes are the same Object is an instance of a class Held by dynamic variable (pointer type) & allocated/deallocated explicitly Allocated via new from heap, deallocated via delete Member access via operator or via dereference + dot: (*obj).field1 =7; operator or via dereference + dot: (*obj)....
View Full Document

Page1 / 11

classes - 5/13/10 1 Composite Types: Classes Similar to...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online