+ 2
I dont able to understand what is abstract class??
10 Respuestas
+ 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.
+ 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
+ 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.
0
Bro can you give me any example throug a program
0
a base class that have one sub class and there is no existence of base class object
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
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
0
try to understand my language