Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class Base
- {
- private:
- string value1;
- int value2;
- protected:
- int protected1()
- {
- cout << "Protected base function" << endl;
- return 0;
- }
- public:
- string getValue1()
- {
- return value1;
- }
- void setValue1(string v)
- {
- value1 = v;
- }
- int getValue2()
- {
- return value2;
- }
- void setValue2(int v)
- {
- value2 = v;
- }
- };
- class Child1 : public Base {
- public:
- void publicFunc() {
- protected1();
- }
- };
- class Child2 : public Child1 {
- protected:
- void protected1() {
- cout << "Protected Child2 function" << endl;
- }
- public:
- void publicFunc() {
- protected1();
- }
- };
- void reverse(int * firstP, int * secondP) {
- while (firstP < secondP) {
- int temp = *secondP;
- *secondP = *firstP;
- *firstP = temp;
- firstP = firstP + 1;
- secondP = secondP - 1;
- }
- }
- int main() {
- int * ptr = (int *)malloc(10 * sizeof(int));
- for (int i = 0; i < 10; i++) {
- *(ptr + i) = i + 1;
- }
- reverse(ptr, ptr + 9);
- int ptr2[10];
- for (int i = 0; i < 10; i++) {
- ptr2[i] = i + 1;
- }
- reverse(&ptr2[0], &ptr2[9]);
- //It operates the same because arrays are just glorified pointers which dont allow you to get out of the allocated space, which since we are not getting out of the allocated space doesnt matter..
- Child1 c;
- c.publicFunc();
- Child2 c2;
- c2.publicFunc();
- Child1 * c3 = &(Child1)c2;
- c3->publicFunc();
- Base * b = &c;
- // b->publicFunc();
- /*
- Child1 instance -> Base func
- Child2 instance -> The non base func
- Child1 pointer -> the base func
- You get an error if you attempt to call the public func because it is not part of the base class
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement