add another polymorphism example
This commit is contained in:
		
							parent
							
								
									9c89f7382a
								
							
						
					
					
						commit
						93ecf568d1
					
				
					 3 changed files with 39 additions and 0 deletions
				
			
		
							
								
								
									
										1
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							| 
						 | 
					@ -21,3 +21,4 @@ rtti
 | 
				
			||||||
future
 | 
					future
 | 
				
			||||||
return-type-deduction
 | 
					return-type-deduction
 | 
				
			||||||
division
 | 
					division
 | 
				
			||||||
 | 
					poly
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -18,6 +18,7 @@ add_executable(classes classes.cpp)
 | 
				
			||||||
add_executable(lvalues lvalues.cpp)
 | 
					add_executable(lvalues lvalues.cpp)
 | 
				
			||||||
add_executable(bad_alloc bad_alloc.cpp)
 | 
					add_executable(bad_alloc bad_alloc.cpp)
 | 
				
			||||||
add_executable(rtti rtti.cpp)
 | 
					add_executable(rtti rtti.cpp)
 | 
				
			||||||
 | 
					add_executable(poly poly.cpp)
 | 
				
			||||||
add_executable(division division.cpp)
 | 
					add_executable(division division.cpp)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
add_executable(future future.cpp)
 | 
					add_executable(future future.cpp)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										37
									
								
								poly.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								poly.cpp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,37 @@
 | 
				
			||||||
 | 
					#include <iostream>
 | 
				
			||||||
 | 
					#include <string>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* abstract */
 | 
				
			||||||
 | 
					class Animal {
 | 
				
			||||||
 | 
					  public:
 | 
				
			||||||
 | 
					    // virtual std::string talk() = 0; /* pure virtual */
 | 
				
			||||||
 | 
					    virtual std::string talk() { ; /* or an implementation */
 | 
				
			||||||
 | 
					      return "<sound>";
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Cat : public Animal {
 | 
				
			||||||
 | 
					  std::string talk() {
 | 
				
			||||||
 | 
					    return "Meow!";
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class Dog : public Animal {
 | 
				
			||||||
 | 
					  std::string talk() {
 | 
				
			||||||
 | 
					    return "Woof!";
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/* Note: need pointer or ref here! */
 | 
				
			||||||
 | 
					void letsHear(Animal& a) {
 | 
				
			||||||
 | 
					  std::cout << a.talk() << std::endl;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int main() {
 | 
				
			||||||
 | 
					  Cat c;
 | 
				
			||||||
 | 
					  Dog d;
 | 
				
			||||||
 | 
					  letsHear(c);
 | 
				
			||||||
 | 
					  letsHear(d);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue