classA { public: voidprintVal(int x){ std::cout << x << std::endl; } }; intmain(){ A* p = new A; p->A::printVal(10); return0; }
6. Access base class members
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classA { public: int x; voidfoo(){ std::cout << "A foo(), x = " << x << '\n'; } }; classB : public A { public: voidsetValue(int value){ A::x = value + 1; // Explicit access to x from the Base class A::foo(); } voidfoo(){ std::cout << "B foo(), x = " << x << '\n'; } };
7. Defining and Using Type Aliases with Namespaces
namespace A { // Type alias for an integer vector using IntVector = std::vector<int>; // Another type alias for a pointer to a function returning int and taking two int parameters using FuncPtr = int(*)(int, int); } intmain(){ A::IntVector numbers = {1, 2, 3, 4, 5}; // Using the type alias for a function pointer defined in MyNamespace auto lm = [](int a, int b){ return a + b; }; A::FuncPtr fptr = lm; int result = fptr(10, 20); return0; }
classA { public: int x = 5; }; classB : public A { // inherits from class A public: int i = 6; }; classC : public A { // inherits from class A public: int i = 7; }; classD : public B, public C { // inherits from both classes B and C }; intmain(){ D obj; std::cout << obj.B::x << std::endl; // Accessing x from B's A std::cout << obj.C::x << std::endl; // Accessing x from C's A return0; }