0% found this document useful (0 votes)
55 views4 pages

Virtual and Static Function

The document demonstrates the use of virtual functions in C++. It defines an Animal base class and Dog and Cat derived classes. Each class has a getType() function that returns the type as a string. A print function takes an Animal pointer and calls getType() on it. When called on objects of the derived classes, the correct overridden getType() is called for each class.

Uploaded by

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

Virtual and Static Function

The document demonstrates the use of virtual functions in C++. It defines an Animal base class and Dog and Cat derived classes. Each class has a getType() function that returns the type as a string. A print function takes an Animal pointer and calls getType() on it. When called on objects of the derived classes, the correct overridden getType() is called for each class.

Uploaded by

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

6. Demonstration of virtual function.

#include <iostream>
#include <string>
using namespace std;

class Animal {
private:
string type;

public:
// constructor to initialize type
Animal() : type("Animal") {}

// declare virtual function


virtual string getType() {
return type;
}
};

class Dog : public Animal {


private:
string type;

public:
// constructor to initialize type
Dog() : type("Dog") {}

string getType() override {


return type;
}
};

class Cat : public Animal {


private:
string type;

public:
// constructor to initialize type
Cat() : type("Cat") {}

string getType() override {


return type;
}
};

void print(Animal* ani) {


cout << "Animal: " << ani->getType() << endl;
}

int main() {
Animal* animal1 = new Animal();
Animal* dog1 = new Dog();
Animal* cat1 = new Cat();

print(animal1);
print(dog1);
print(cat1);

return 0;
}

Output

Animal: Animal
Animal: Dog
Animal: Cat
7. Demonstration of static function.

#include <iostream>  
using namespace std;  
class Account {  
 public:  
 int accno; //data member (also instance variable)      
  string name;   
static int count;     
Account(int accno, string name)   
  {    
   this->accno = accno;    
 this->name = name;    
  count++;  
}    
 void display()    
 {    
  cout<<accno<<" "<<name<<endl;   
}    
};  
int Account::count=0;  
int main(void) {  
  Account a1 =Account(201, "Sanjay"); //creating an object of Account  
 Account a2=Account(202, "Nakul");   
Account a3=Account(203, "Ranjana");  
  a1.display();    
a2.display();    
 a3.display();    
 cout<<"Total Objects are: "<<Account::count;  
  return 0;  
}  

Output:
201 Sanjay
202 Nakul
203 Ranjana
Total Objects are: 3

You might also like