C++ Class, Why?
Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: C++ Class, Why?

  1. #1
    Member
    Join Date
    Feb 2003
    Posts
    78

    C++ Class, Why?

    Ok, I am moving ahead in my C++ book. Because of this helpful tool, I have passed my programming class and have no one to turn to but the AO community. Now I am on classes. I kinda understand the syntax, for the most part. But what I cant see is where I would use this. I guess what I am trying to say is, what is the purpose? Where would you use classes. In the book it explains how to create and use them, but it only give and example about class Cat. So maybe someone can provide some information on what you use classes for. Are they just for good programming?

    Also, while talking about C++ classes, I was wondering what the constructor and destructor does. I understand you would be like,
    Cat::Cat()
    and
    Cat::`Cat \\Tilde
    What are these and what do they do?
    Thanks in advance.

    -Ep
    01001001001000000100110001101111011101100110010100100000010000100110010101110100011101000111100100100001

  2. #2
    A quick and short answer to that in english would be:

    Using classes have 2 main advantages;
    1 - They add structure to your program so that its not just a long list of code commonly referred to as 'spaghetti code'

    2 - They can greatly decrease the amount of coding needed for a full program

    A quick description of #2, pretend you're writing a program that has to do 1 certain thing many many times, by defining a class you only have to code the thing once and then just call the class everytime you want the program to do it, without a class you'd have to code that same chunk of code into your program over and over again...

    Hope that makes sense - its a very brief description of the basics of a class...

    Here is a link to a place that shows a quick example of using classes (and i believe is better then the Cat example) its the 'Dog' example

    http://cplus.about.com/library/weekly/aa041002d.htm

    RRP

  3. #3
    Senior Member
    Join Date
    Sep 2003
    Posts
    179
    Classes are at the heart of OOP (object oriented programming). Classes are blueprints for Objects. Classes tell the compiler what an Object is made of (ie functions and variables). By using options it helps simplify programming, by basing programming on real world objects. For example you have class named car. In car you have variables for gas_level, and oil_temp. For functions in car you have startEngine() and shiftGear(). Now u use this class to create a object named Ford. Now you have a ford car. I could go into much more detail but i'm in class.

    In recap you use classes to define objects. You create objects to simply programs by programming using real world objects. It as consepuial way to make programming easier.

    DeafLamb

  4. #4
    Junior Member
    Join Date
    Oct 2003
    Posts
    18

    for network/sys admin

    In our Novell Cert class, we just got done learning about objects........classes, types, instances......you can use classes/methods/values for login scripts, assigning rights......C++ is used to teach about object oriented.....kinda like learning how to drive..........C++ is your ferarri.Java is your astro van,...........now C++ is learning how to drive an automatic while learning C is learning how to drive a manual........I hope I made some sense for you

  5. #5
    GreekGoddess
    Guest
    I don't know if this would help much, but maybe a web example might be useful as an addition to what was already stated. If you've ever used CSS and call to a class or ID, it's the same principle.

    CSS side:
    div.title {
    font-size:x-small;
    font-weight:normal;
    font-variant:capitalize;
    color:#FFFFFF;
    text-align:left;
    font-weight:bold;
    padding-top:2px;
    border-bottom:solid 1px
    border-color:#FFFFFF;

    }
    HTML side:
    (use the class variable whenever you want to call to your title format)

    <div class="title">1st Title</div>



    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean sed neque ut leo lacinia interdum. Ut ac tortor et ligula rutrum ultricies. Proin auctor gravida pede. Aenean quis ligula nec ligula ultrices vestibulum. Nam eu diam. Duis urna lorem, scelerisque ac, aliquet a, varius vitae, nisl. Aliquam ullamcorper turpis non pede. Donec massa. Sed sem lacus, ultrices eu, convallis at, accumsan quis, ante. Nunc ullamcorper posuere dui. Donec at diam. Etiam ipsum. Aenean ultricies tortor ut tellus. Sed eu lorem.
    </p>

    <div class="title">2nd Title</div>



    Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean sed neque ut leo lacinia interdum. Ut ac tortor et ligula rutrum ultricies. Proin auctor gravida pede. Aenean quis ligula nec ligula ultrices vestibulum. Nam eu diam. Duis urna lorem, scelerisque ac, aliquet a, varius vitae, nisl. Aliquam ullamcorper turpis non pede. Donec massa. Sed sem lacus, ultrices eu, convallis at, accumsan quis, ante. Nunc ullamcorper posuere dui. Donec at diam. Etiam ipsum. Aenean ultricies tortor ut tellus. Sed eu lorem.
    </p>
    Like bpiedlow said, it decreases the amount of code and makes it easier to call to variables without having to type them over and over again. It's also nice, because, like a stylesheet, to change ALL the titles, all I have to do is go into the stylesheet itself and edit that one specific class and it changes every "title" class with the new variables.

  6. #6
    Member
    Join Date
    Feb 2003
    Posts
    78
    Alright, so classes are like functions. Classes are involved with OOP, which I havent fully gotten yet. If I call a class over and over again, like in your font example, cant I just make the class a function, or would that defete the whole purpose of OOP?

    -Ep
    01001001001000000100110001101111011101100110010100100000010000100110010101110100011101000111100100100001

  7. #7
    Junior Member
    Join Date
    Oct 2003
    Posts
    18
    you could, but it would kinda defeat the purpose(calling a method is also using OOP) of using the class.....but you have to have a class defined to do java programming while using C++, one doesn't need a class.........but classes are great to use, and need to use when making complex programs(simulators) instead of simple programs(hello world)

  8. #8
    Senior Member
    Join Date
    Sep 2003
    Posts
    179
    Lets take in from a practical example.

    Say you were hired by a company to program a payroll system. ( A trite and played out example I know, but it works well.)

    First you ask your self, "What will this program need to work?"
    1. A complete list of all employees.
    2. The employee's Peronal Data: (SS, Name, Address etc)
    3. The employee's Hourly Rate.
    4. The hours the person worked.
    These are all the 'varialbes' you will need' there may be more, but this is just a simple example.

    Second you ask your self, "What will this program have to do, to work."
    1. Clock employee's in and out.
    2. Save information to a Database file
    3. Figure out Payroll
    4. Print checks.
    These are the 'functions' that your program will need to operate on its variables.

    Now Lets build a class.

    A class (in C++) is a definition for an object, it describes what data types an object will contain, and what functions it will have to work on this data.

    IMPORTANT in C++ (as far as I know) you cannot use a class by itself, a class is only a definition, you must create an object to use these variables and functions you have created.

    So in our Employee class we define the following variables.
    EmployeeName, HourlyPay, HoursWorkedTotal, ClockInTime, ClockOutTime, etc....
    Also in our Employee class we define the folowing Functions.
    ClockEmployeeIn(), ClockEmployeeOut(), AddTimeToTotal(), PrintCheck(), etc...

    now we have a class that defines our employee, so in our program we will create as many employee objects that we need using 'new'

    So say we created an object named Bob.

    Bob now has all the variables that we defined in our Employee Class.
    Bob also can use all the functions that were defined in our Employee Class.

    Well now your saying, yeah but I could of just wrote the functions, and then created BobHourlyRate, BobTotalHours, and such. The truth is you could of, but now look at this.

    We need to add another employee. Janet. Now we use new to create and object named Janet based on the Employee class. See how easy that was, using classes and objects suports code reusability. It's really neat once you get into inheritence and polymorphism, but that is beyond the scope of one thread.

    So to recap, Classes (in C++) are the bluprints for objects, they define how your object will be layed out (what variables and functionis it will contain) by writing just one class, and creating multiple objects you are using the same code over and over, which makes the code easier to understand, and maintain.

    Hope I helped

    DeafLamb


    EDIT: I just read the second part of the question asking what a constructor and destructor does. You really need to read a book to get all the info your trying to gain from this thread.

    Quick explanation, they do exactly what they say. A constructor creates an object from a class definition. You can have multiple constructors, which is the base of polymorphism (way beyond scope of this thread). This construtor builds the object, you can write your own constructor, or a default constructor is created for you by the compiler.

    A descructor destroys an object, this releases all the memory used by the object. There is only one destructor, and it takes no arguments. If you don't specifically write one, a default destructor is also created by the compiler.

    DeafLamb

  9. #9
    Member
    Join Date
    Feb 2003
    Posts
    78
    Thank you very much all of you. Deaflamb, your explanation on the payroll really helped me out. I will try to find a nice site on constructors and destructors. Thanks again.

    -Ep
    01001001001000000100110001101111011101100110010100100000010000100110010101110100011101000111100100100001

  10. #10
    Senior Member
    Join Date
    May 2003
    Posts
    226
    Originally posted here by Epison07
    Thank you very much all of you. Deaflamb, your explanation on the payroll really helped me out. I will try to find a nice site on constructors and destructors. Thanks again.

    -Ep
    here's a simple and nice one

    http://www.cplusplus.com/doc/tutorial/tut4-1.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •