Skip to content

Commit

Permalink
add: c++ virtual override
Browse files Browse the repository at this point in the history
  • Loading branch information
hthuz committed Sep 11, 2024
1 parent d6fd6b8 commit e77d832
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
c/*.out
c/*.o
cpp/*.out
cpp/
!cpp/*.cpp


java/*.class
Expand Down
23 changes: 23 additions & 0 deletions cpp/override.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#include <iostream>


class Expression_class {
public:
// Without virtual, the program will gives 0; => Based on declared type
// With virtual => Based on derived classes
virtual bool is_no_expr() {return false;}
};

typedef Expression_class* Expression;

class no_expr_class : public Expression_class {
public:
bool is_no_expr() {return true;}
};

int main() {
// Compiler makes decision based on static type information available at compile time
Expression expr = new no_expr_class();
std::cout << expr->is_no_expr() << std::endl;
}
26 changes: 26 additions & 0 deletions cpp/string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

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

int main() {
string str;
str = "amazing";
cout << str << endl;
str = "amaz\"ing";
cout << str << endl;
str = "amaz\"in\"g";
cout << str << endl;
str = "amazi""ng";
cout << str << endl;
str = "amaz'ing";
cout << str << endl;
str = "ama'z'ing";
cout << str << endl;
str = "ama\'zing";
cout << str << endl;



}

0 comments on commit e77d832

Please sign in to comment.