+ 2

I dont able to understand what is abstract class??

17th Feb 2017, 2:23 PM
Mukesh Kumar
Mukesh Kumar - avatar
10 Antworten
+ 6
you cannot create an instance of an abstract class. it is only good as superclass for normal classes. EG: a pet.... you dont have a pet ...you have a cat or a dog or whatever. pet is abstract. cat and dog are normal classes in my example.
17th Feb 2017, 4:19 PM
Oma Falk
Oma Falk - avatar
+ 6
To pick up @Oma Falk example with the pets here's some code. I did it quickly, so it's not that clean: #include <iostream> using namespace std; //the Pet class is an abstract class, i cant be instantiated. if you try, compiler will complain class Pet { public: string Name; //Constructor, is only called by base classes Pet(string name) { Name = name; } //this method makes the class an abstract class, it has no implementation, derived classes are supposed to do this virtual void makeNoise() = 0; }; class Cat : public Pet { public: //Constructor, base class constructor is called for name assignment Cat(string name) : Pet(name) {} //the Cat-Class needs to implement the makeNoise function to work void makeNoise() { cout << "Cat "<<Name<<" makes Meow!\n"; } }; class Dog : public Pet { public: //Constructor, base class constructor is called for name assignment Dog(string name) : Pet(name) {} //the Dog-Class needs to implement the makeNoise function to work void makeNoise() { cout << "Dog "<<Name << " makes Woof!\n"; } }; int main() { //an array of 4 pets const int amount = 4; Pet *pets[amount]; //add some pets dynamically, so we need to delete them later to free the memory again pets[0] = new Dog("Charlie"); pets[1] = new Cat("Kitty"); pets[2] = new Dog("Struppy"); pets[3] = new Cat("Mary"); for(int i=0; i<amount; ++i) { pets[i]->makeNoise(); //we dont need the pet anymore :( delete delete pets[i]; } return 0; } Here's the link to the playground: https://code.sololearn.com/ct5B70O68B8g/#cpp
17th Feb 2017, 8:56 PM
lulugo
lulugo - avatar
+ 3
An abstract class is a class that cannot be instantiated and must at least contain one abstract method. An abstract method is a method that is only declared, but without implementation. You use abstract classes to generalize. If you want to make a bunch of classes that all have several methods in common, but which may work for ervery class a bit different, the best way to do this is to create an abstract class and then derive the classes from the abstract class. The classes that derive from the abstract class need to define the abstract method from the base class. A class containing only abstract methods and nothing else is called an Interface.
17th Feb 2017, 8:49 PM
lulugo
lulugo - avatar
0
Bro can you give me any example throug a program
17th Feb 2017, 3:30 PM
Mukesh Kumar
Mukesh Kumar - avatar
0
a base class that have one sub class and there is no existence of base class object
22nd Feb 2017, 10:50 AM
sanjay
sanjay - avatar
0
Bro.. Abstract means.. hiding unnecessary things.. shows only necessary things.. Real world ex : Tv remote.. we can see only switches on remote which is necessary.. we couldn't see the inner circuits and inner parts of remote.. Ex prblm : abstract class car // abstract class { abstract void engine(): /**This method must be defined in sub class which extends abstract class)*/ void speaker() void ac() } class audi extends car { void engine() { System.out.println("Audi Engine"); } void ac () { System.out.println("Audi Ac"); } } class exec { public static void main(String[ ] args) { audi a1=new audi(); a1.engine(); audi a2= new audi(); a2.ac }} in this example .. Every car should need Engine .. but ac and speakers are not much important.. Interface is one of the pure example abstraction
22nd Feb 2017, 2:36 PM
imran naseer
imran naseer - avatar
0
a class, that serves only as a base class from which other classes are derived,but on object of this base class type exist, is known as abstract class
23rd Feb 2017, 1:28 PM
sanjay
sanjay - avatar
0
try to understand my language
23rd Feb 2017, 1:28 PM
sanjay
sanjay - avatar