













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to inheritance and function overloading in c++. Inheritance is a way to define objects in terms of hierarchical classes, where each level inherits the attributes of the level above it. Function overloading allows writing multiple functions with the same name but different argument lists, enabling the same function to perform similar tasks on different data types. The document also covers function templates and operator overloading.
Typology: Slides
1 / 21
This page cannot be seen from the preview
Don't miss anything!














Lecture 28
class A : base class access specifier B { member access specifier(s): ... member data and member function(s); ... }
Valid access specifiers include public, private, and protected
public base class (B)
public members protected members private members
derived class (A) public protected inherited but not accessible
class A : public B { // Class A now inherits the members of Class B // with no change in the “access specifier” for } // the inherited members
private base class (B)
public members protected members private members
derived class (A) private private inherited but not accessible
class A : private B { // Class A now inherits the members of Class B // with public and protected members } // “promoted” to private
class Shape { public: int GetColor ( ) ; protected: // so derived classes can access it int color; }; class Two_D : public Shape { // put members specific to 2D shapes here }; class Three_D : public Shape { // put members specific to 3D shapes here };
int main ( )
{
Square mySquare; Cube myCube;
mySquare.getColor ( ); // Square inherits getColor() mySquare.getArea ( ); myCube.getColor ( ); // Cube inherits Docsity.com
void swap (int *a, int *b)
{ int temp; temp = *a; *a = *b; *b = temp; }
void swap (float *c, float *d)
{ float temp; temp = *c; *c = *d; *d = temp; }
void swap (char *p, char *q)
{ char temp; temp = *p; *p = *q; *q = temp; }
template
{
T temp; temp = *a; *a = *b; *b = temp;
}
T is a “dummy” type that will be filled in by the compiler as needed
a and b are of “type” T temp is of “type” T swap is a function template, NOT a function
int a = 5, b = 6; float c = 7.6, d = 9.8; char e = 'M', f = 'Z'; swap (&a, &b); // compiler puts int in for T swap (&c, &d); // compiler puts float in for T swap (&e, &f); // compiler puts char in for T Docsity.com