Abstract Data Types and Constructors Question

Right now my class is doing work on ADT and I have read through the section of the textbook that we are using about ADT and constructors several times and I'm still lost about their concept and using them. Can someone explain those two terms to me and how they are used? From what I know so far, ADT are functions that are done to data types that you can create. Constructors, to the extent of my knowledge, is a member function within a class that has the same name as the class but I have no clue how to use or create them, nor do I know what they do. Can someone help me out? Thanks!

Please dont post anything telling me that homework is my own responsibility, I've tried and looked around the internet for information about these things but nothing so far. I just need a general understanding of these things
Last edited on
An Abstract Data Type is, exactly as it describes, a data type usually built using classes in C++, used for functional means that are beyond that standard C++ data types. A perfect example of this would be the linked list ADT.

A constructor is something that runs when an instance of a class is created. Every class has a default constructor, which will run if you don't create your own. The idea of a constructor is to initialise class values upon creation of the class.

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
class MyClass
{
private:
   int data;
public:
   MyClass(): data(5) // Constructor, same name as class, no return type
}


// In main...
MyClass class1;  // Static instance. Constructor called here.
MyClass* class2; // Dynamic instance.
class2 = new MyClass; // Construct called here. 


In my code above, the constructor sets data to 5 whenever an instance of the class is created. My constructor there is using an initialization list, which sets the attributes after the colon. It could also be written like this:
1
2
3
4
MyClass()
{
   data = 5;
}


Hope this helps.
Last edited on
Topic archived. No new replies allowed.