Computing.Net > Forums > Programming > Classes in C++

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Classes in C++

Reply to Message Icon

Name: Seth Miller
Date: November 8, 2004 at 09:33:52 Pacific
OS: A good one
CPU/Ram: Pretty good
Comment:

I have been trying to learn C++ for a while now. I found a pretty good tutorial online titles "Teach yourself C++ in 21 days". I understand it very well up to chapter 6 "Classes". I have read this chapter over and over and I still don't understand it. I searched for other docs on just classes but I am still stumped. I thought about pressing on and skipping it but I really want to learn all there is to know. Does anyone have any good docs or books that would help? Do you think I can skip classes for now or is it really important? Thanks in advance. Seth



Sponsored Link
Ads by Google

Response Number 1
Name: Don Arnett
Date: November 8, 2004 at 11:24:55 Pacific
Reply:

I don't think that you want to skip classes. The major difference between C and C++ is that C++ supports object oriented programming and object oriented programming depends upon classes.

You might try restarting chap 6 and posting specific questions here as you come across things that you don't understand.


0

Response Number 2
Name: gimmpy224
Date: November 8, 2004 at 15:25:44 Pacific
Reply:

Ahhh yes i remember posting about the same problem.

Trust me in C++ classes are a must.

If you have learned about structs then you already knows what a class somewhat does.

But just incase you havnt ill take you over everything in classes (mostlyISH) because i got a lot of free time :-D.

Making a class is like filling out an index card. (structs are the same way up until the OOP part of it).

class Name
{
public:
char *firstname;
char *lastname;
char *eyecolor;
char *haircolor;
};

^^^ That is how you "prototype" (not sure if thats correct syntax so dont hold me to it)
a class.

So now you have a class that can hold information about someone.
To access that information you must istantiate an object from that class.

Example:
//type your prototyped class here just below
//the includes.

void main()
{
Name bill;
return;
}

bill is your object that is istantiated from the Name class.
Now bill holds each attribute within itself that Name does, its almost like making your own datatype.

If you want to imput data to bill you could do as follows:

void main()
{
Name bill;

bill.firstname = "William";
bill.lastname = "Wallis";
bill.eyecolor = "Hazel";
bill.haircolor = "Black";

return;
}

The (.) you use to access the attributes of bill is called the DOT operator.

Now if you wanted to output the values of bill to the screen you could do as follows:

void main()
{
Name bill;


bill.firstname = "William";
bill.lastname = "Wallis";
bill.eyecolor = "Hazel";
bill.haircolor = "Black";

cout << "First name is : " << bill.firstname <<endl;

cout << "Last name is : " << bill.lastname <<endl;

cout << "Eye color is : " << bill.eyecolor <<endl;

cout << "Hair color is : " << bill.haircolor <<endl;

return;
}

So once you have an object of your class you must you the DOT operator to access its members in anyway ( or the pointer operator (->)).

Anothing classes can do is hold functions within itself.
Having a class use its own function can be useful for Object Oriented Programming.

The basic idea of Object Oriented Programming is to make your code be very easy and fluent to understand. You may think this is a waste, but if you wanna get into MFC (I am right now and dont Listen to Raja lol) then your going to need to know all you can about OOP.

So lets say you wanted to print and input some values into some information in your class.
You could write a function that prints your information and that inputs the info into the datatypes.

EX:

class Name
{
public:
char *firstname;
char *lastname;
char *eyecolor;
char *haircolor;
void GetInfo();
void PrintInfo();
};

Name::GetInfo()
{
cout << "What is your first name? ";
cin >> firstname;
cout << "What is your last name? ";
cin >> lastname;
cout << "What color are your eyes? ";
cin >> eyecolor;
cout << "What color is your hair? ";
cin >> haircolor;
};

Name::PrintInfo()
{
cout <<"Your first name is " << firstname <<endl;

cout << "Your last name is " << lastname <<endl;

cout <<"Your eyes are " << eyecolor <<endl;

cout <<"Your hais is " << haircolor << endl;
};

void main()
{
Name bill;
bill.GetInfo();
bill.PrintInfo();

return;
}

You would want to do this so you can follow your code very easily.

Which brings me to the point of encapsulation.
Encapsulation is important because it keeps the code from being tampered with by unknown and outside objects.


Inheritance - In classes you can have a BASE class.

Sometimes youll have some code that youll want to reuse for a class, but then youll want to add on to it.

For example:

class Automobile
{

};

Every car has features on it that are manditory right?
Well then you could prototype automobile like so :

class Autmobile
{
public: <<< ill discuss this in a bit
char windows;
char *color;
char *engine;
};

Each automobile has windows, color, and an engine right? But what if you wanted perhaps a car with things that were only available to it, but it has all the assets any normal automobile has?
Thats what inheritance is for.
With inheritance you can DERIVE a class from Automobile and still be able to access and add onto your DRIVED class.

For example:

Class Taurus : public Autmobile
{
char CDplayer;
char LeatherSeats;
};

Now your class taurus has all the same attributes as an automobile (windows, color, and an engine) but it also has its own special attributes along with those (CDplayer and leather seating).

Im sorry but I got a lot of hw so ill post more later if you need it, also i here the sams book your reading for C++ isnt good at all, I learned from reading Visual C++ in 12 easy lessons :)

GIMPS


0

Response Number 3
Name: gimmpy224
Date: November 8, 2004 at 17:53:40 Pacific
Reply:

Mk anyways, like i said earlier the difference is between a struct and a class is that a class can have private and protected members?
Well those two words are what encapsulation is.
Private and protected keep other code from using those datatypes and/or functions.

An example:

#include <iostream.h>

class MainClass
{
private:
int prvtint;
public:
int pubint;

};

void main()
{
MainClass Object;

Object.prvtint = 5;

Object.pubint = 5;

return;
}

In this code you would get an error because prvtint cannot be accessed buy the object because it is private.
If it is declared private then nothing can mess witht hat particular code except the class itself.
Now public on the other hand lets any code tamper with it.
So if you wanted to keep code secure and make sure it cant be changed because if its changed youll get errors or the wrong output, private would be the way to go.
This also helps people find the error in the code because if that code cannot be fooled with except in that class then that could narrow your search down for that error a great amount.

Now protected is used in inheritance.

If you use private in a BASE class and you derive a class from that base class, then the datatypes and/or functions that were declared private cannot be manipulated in that derived class.
BUT protected on the other hand will let the code be manipulated in the derived class, but cannot be manipulated outside the derived class or its base class.

So all in all classes help you keep your code together and readable by letting you use Object Oriented Programming.

And encapsulation is always good because like i said, if that class is the only thing that can manipulate that data, then there is less chance for an error and your code is nice and reable if you ever decide to add on to it.

GIMPS


0

Response Number 4
Name: Seth Miller
Date: November 8, 2004 at 22:58:38 Pacific
Reply:

Thanks a lot guys. I understood the classes thing up until they started talking about accessor funtions. I think the text is riddled with a lot of typos though. I have caught many and had to correct the sample code it gave. And thank you gimp your examples were much easier to follow. What other books did you use to get started? You mentioned VC++ in 12 easy lessons but I am using Dev-C++ does it matter? Again thanks for the help.


0

Response Number 5
Name: gimmpy224
Date: November 9, 2004 at 15:40:16 Pacific
Reply:

well if you want... i got some STUFF that i can send you, if i can figure out how.... (visual c++ 6.0 and about 1000 ebooks on all sorts of languages :) ).

But yes mainly i used the C++ in 12 easy lessons and the guys on the forums to help me out :).

And anytime you need anything just ask, people here on the forums are very friendly.

GIMPS


0

Related Posts

See More



Response Number 6
Name: gimmpy224
Date: November 9, 2004 at 21:21:25 Pacific
Reply:

And my bad i didnt answer your question lol.

I dont think that the C++ syntax changes from compiler to compiler, the code is the same but the compilers all have different interfaces.

:) and i figured out how to get you all the stuff i have.... just email me at anarchistgamer@comcast.net and we can talk about it.

GIMPS


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Classes in C++

Simple design questions in C++ www.computing.net/answers/programming/simple-design-questions-in-c/4384.html

getline in c++ www.computing.net/answers/programming/getline-in-c/11362.html

Problem reading file in c++ www.computing.net/answers/programming/problem-reading-file-in-c/7826.html