Answer to Question #283292 in C++ for Rasheed

Question #283292

Question 5 (20 marks) avoid plagiarism.

Discuss Object oriented programming in c++ with 4 points. Each point shoult not be less

than 200 words.


1
Expert's answer
2021-12-31T01:42:25-0500

Object oriented programming is a programming model, which describes a model of organising data in programming.

  • Main attributes of this model are "object" and "class". Class is a user-defined type of data. We can create some objects of that type. For example, we are designing a class "Apple", so objects of that class will be some particular variables of type apple, so they will be some apples. By the way, there are many examples of such approach. Types can be various: human, vehicle, figure and so on. We have to remember, that while creating a class there should be some common things for the objects, for example, if we design class Figure, the objects: rectangle, square, circle etc. will have different features, because their perimeter and square functions will be different.

class apple {}; //this code means we declared and initialised a class (user - defined type)

apple a; // this code means we declared an object of type apple.

Now let us describe the main principles of object oriented programming

  • Incapsulation - this principle means hiding some inside realisation of class and just giving a user of a particular class some instruments he needs and nothing more. Thus, user can not influence on key features of the class. He can just use functions to distinguish one object from another. Incapsulation always mean a focus on some correct things which can be used and modified and never allows to see how some things are developed and gives no possibility to modify this things. Such things can be implemented by using availability key words like private, protected and public. Private modificator means user can not see functions or fields by this key word directly using . operator. Public modificatory means functions and fields are visible from object, can be modified and used in any way. And protected key word means fields and methods can not be seen and used from object, but can be inherited by son class and get private key word.

class Apple

{

private: // can not be seen or modified from object, can not be inherited

protected: // can not be seen or modified from object, can be inherited

public: // can be seen or modified from object, can be inherited

};

  • inheritance - very useful principle in object oriented programming which means one class can be inherited by another. So when we implement one particular class we can inherit another class from it. That means son class will have all the features basic class has (all fields and methods), so in son class we can reimplement some things or add some key features in a new class. For example we have class tree which has some features like age, height, has methods which calculates something and displays and we want to have another class ChristmasTree, which would like to have same things, which class tree has but also wants to have some unique method "Shine", so the best way would be not copying and pasting code to the new class, but just inheriting from base class. Here we also have key words which define the level of inheritance and which availability inherited fields and methods in the new class will have. Here we also have three options of inheriting: different key words (private, public and protected). The most common key word, used here is public. It means after inheritance everything left the same: protected fields remain protected, public remain public and private fields and methods are unaccessible in new class. If we use private key word, public and protected fields and functions became private in a derived class, private fields are unaccessible, and by using protected key word we convert public fields in protected fields and private fields are still unaccessible.
class Tree
{
protected:
string colour;
float height;
int age;
};

class ChristmasTree : public Tree // this code means we inherited from base class Tree
{
// here we also have colour, age and height by inheritance;
void Shine()
{
cout<<"I'm shining"<<endl;
}
};
  • polymorphism. This principle means we can define what method to use in runtime. One of the things where we use prime option of polymorphism is function overloading. In this case the compiler doesn't know which function we will call, because it has several functions with the same name but different argument types or different argument number. Thus, we call function and now program knows which function to call. And the most common way of using polymorphism is by using virtual functions. In this case both base and inherited classes have the functions with same signature (type of returned value, name, argument count and type), but have different function bodies. For this purpose we have to use virtual key word. When we implement a function in a base class first we write virtual word and the function definition, in inherited class we also can use virtual key word to indicate that this function points to polymorphism, also for best practices it is required to write override key word to let the compiler check our work in the case we missed something. To make everything work we have to declare a pointer to the base class and initialise it with memory address of derived class. In this case the function from derived class will work. This can be decided in runtime. Also in C++ we don't have abstract classes but we can implement it by writing a pure virtual functions in a base class. This way we indicate that no object can be created from this class, but this method have to be implemented in a derived class.
class Base
{
public:
virtual void Print()
{
  cout<<"Hello from base"<<endl;
}
};
class Derived: public Base
{
public:
void Print() override
{
  cout<<"Hello from derived"<<endl;
}
};
int main()
{
Derived d;
Base* b = &d;
b->Print(); //Hello from derived
}



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog