0% found this document useful (0 votes)
12 views15 pages

6

Create a Derived Class “Professor” that inherits from both Personal and Official classes. • Professor: Personal, Official, Department, Designation Write a program to take Professor details as input and display it.

Uploaded by

Muhammad Sarmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views15 pages

6

Create a Derived Class “Professor” that inherits from both Personal and Official classes. • Professor: Personal, Official, Department, Designation Write a program to take Professor details as input and display it.

Uploaded by

Muhammad Sarmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Static Local Variables

Static local variables are used when it is necessary for a function


to remember a value of a variable.

A static local variable has the visibility of an automatic local


variable.

Its life time is same as global variable except that it does not
come into existence until the first call to the function containing
it.

The declaration of a static variable begins with keyword “static”.


Example:
float getavg (float); float getavg (float newdata)
void main( ) {
{ static float total = 0;
float data=1, avg; static int count = 0;
while (data!=0) count++;
{ total+=newdata;
cout<<“\nEnter a number”; return (total/count);
cin>>data; }
avg = getavg(data);
cout<<“\nNew Avg is”<<avg;
getch( );
}
}
Static Variables
An object of a class has its own copy of all the members of the
class.

Static variables are used when only copy of a variable should be


shared by all objects of a class.

The declaration of a static member begins with keyword “static”.

Static members can be declared public, private or protected.

It is visible only within a class but its lifetime is the entire


program.
Example:
class foo int foo::count = 0;
{
private: void main( )
static int count; {
public: foo f1, f2, f3;
foo ( ) cout<<“count:”<<f1.getcount();
{ cout<<“count:”<<f2.getcount();
count++; cout<<“count:”<<f3.getcount();
} }
int getcount( )
{ Static data members require two
return count; separate statements. The variable’s
} declaration appears in the class
definition but the variable definition
};
outside the class in same way as
global variable.
Static Functions

The static member functions are not used very frequently in


programs. But nevertheless, they become useful whenever we
need to have functions which are accessible even when the class
is not instantiated.

Static member functions are considered to have class scope. In


contrast to non-static member functions.
Differences between a static member function and non
static member functions:

1. A static member function can access only static


members and data and functions outside the class.
2. A non-static member function can access all of the
above including the static data member.

3. A static member function can be called, even when a


class is not instantiated.
4. A non-static member function can be called only after
instantiating the class as an object.
Example:
# include <iostream.h> int gamma :: total = 0;
# include <conio.h> void main( )
class gamma {
{ clrscr();
private: gamma g1;
static int total; gamma :: showtotal();
int id; gamma g2,g3;
public: gamma :: showtotal();
gamma ( ){ g1.showid();
total++; g2.showid();
id=total; } g3.showid();
cout<<“END OF PROGRAM”;
static void showtotal( ) getch( );
{cout<<“Total: ”<<total; } }
void showid()
{ cout<<“id:” <<id; }
};
INHERITANCE

• Inheritance is another key feature of Object Oriented


Programming (OOP).

• When creating a class, instead of writing completely


new data members and member functions, the
programmer can designate that the new class should
inherit the members of an existing class.

• This existing class is called Base Class and the new


class is called Derived Class.
BASE CLASS DERIVED CLASSES

Student GraduateStudent, UndeGraduateStudent,


CollegeStudent

Shape Circle, Triangle, Rectangle, Cube, Square

Employee Faculty, Staff

Account CheckingAccount, CurrentAccount,


SavingAccount
Inheritance relationship form treelike hierarchical structures as shown
below. A class can be Base Class, Derived Class or both.

Shape

ThreeDimensional
TwoDimensional
e

Circle Square Cube Sphere


Example 1:
class Counter { void main ( ) {
protected: CountDn c1;
int count; ++c1; ++c1; ++c1;
public: cout<< “\nc1=”<<c1.getCount();
Counter ( ) - -c1; - -c1;
{ count = 2;} cout<< “\nc1=”<<c1.getCount();
Counter (int c) }
{ count = c;}
int getCount( )
{ return count; }
void operator ++( )
{ ++count; }
};
class CountDn:public Counter{
public:
void operator - - ( )
{ --count; } };
Example 2:
# include <iostream.h> class Student:public Bahria {
# include <conio.h> protected: int id;
class Bahria { public:
void getdata( ) {
protected: Bahria::fname();
char fnm[20]; cout<<"\nEnter Id: ";
cin>>id; }
public: void showdata( ) {
void fname( ) Bahria::showdata( );
{ cout<<"\nEnter First Name: "; cout<<"\nId: "<<id; }
cin>>fnm; } //void bye( ) {
void showdata( ) //cout<<"\nBye Student!";} };
{ cout<<"\nFirst Name : "<<fnm; } void main( ) {
void bye( ) Student s;
{cout<<"\nBYE Bahria!";} s.getdata( );
}; s.showdata( );
s.bye( ); }
Assignment
• Make a class of vehicle as a base class, derive classes
of motorbike, car, truck, crains, lifters from the vehicle
class. Write a program that will generate the list of
any type of vehicle according to the choice plus the
user can also search the desired vehicle by its
company name say user can search all cars of Toyota
etc.
MULTIPLE INHERITANCE
A class can be derived from more than one class.

class A
{ };
class B
{ };

class C : public A, public B


{ };
Class Assignment
• Create Two Base Classes “Personal” and “Official”.

• Personal: Fname, Lname, Age, HomeAddress


• Official: OfficeAddress, Publications,

You might also like