Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 9th, 2006, 5:08 PM   #1
funkey_monkey
Programmer
 
Join Date: May 2006
Posts: 34
Rep Power: 0 funkey_monkey is on a distinguished road
Framed Inputs

Hi,

I've been muddling my way through the following example and I've got some questions on it which I hope you can answer simply.

// Ask for a persons name and greet this person
#include <iostream>
#include <string>

int main()
{
    // Ask for the person's name
    std::cout << "please enter your first name: ";

    // Read the name
    std::string name;   // Define name
    std::cin >> name;

    // Build the message we intend to write
    const std::string greeting = "Hello, " + name + "!";

    // Build 2nd and 4th lines of the output
    const std::string spaces(greeting.size(), ' ');
    const std::string second = "* " + spaces + " *";

    // Build the 1st and 5th lines of the output
    const std::string first(second.size(), '*');

    // Write it all
    std::cout << std::endl;
    std::cout << first << std::endl;
    std::cout << second << std::endl;
    std::cout << "* " << greeting <<" *" << std::endl;
    std::cout << second << std::endl;
    std::cout << first << std::endl;

}

Questions
1. Name is a variable. A variable is an objectthat has a name. An object, in turn, is a part of the computer memory that has a type. (So says my book).
I can understand that Name is a variable - as it can take different inputs values, hence it is a variable. I also undertstand that its type is a string (same way that Pi is a numeric type, etc).
However, the object part is confusing me. Can someone please clarify what an object is?

2.
const std::string spaces(greeting.size(), ' ');
My understanding of this line is that spaces is a constant (as opposed to variable) of type string. I am not familiar however with what is going on inside the brackets. Can someone please explain it to me?

3. Is there not a quicker/simpler/more efficient (in terms of compiler time and memory usage) implementation to do this task?

Tthanks,

FM.
funkey_monkey is offline   Reply With Quote
Old Oct 9th, 2006, 5:45 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
An object is a thing.

Your second question uses a form of initializer that allows you to put a character ('a', not "a") in a string. The first argument is a length specification. Greeting.size () is a method that returns a length. If it returns the quantity, 5, you will put 5 spaces into the string.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 9th, 2006, 5:47 PM   #3
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
1) An object is an instance of a class. Think of the computer you're using right now. Computers in general are classed as things with certain characteristics such as the processing power, and certain operations they can perform. Your own personal computer is an object of the computer class with its own custom information that defines itself.

A string object is an instance of the string class. It has information associated with it such as its length, and there are certain operations that are associated with it, such as concatenation.

OOP is the practise of creating classes that define certain things and then creating instances of those classes.

2) The const keyword in front of the string instance tells the compiler (and other bits of the program ) that after it has been given an initial value, the object cannot be modified.

Inside the brackets, the string constructor is called. A constructor for a class creates new objects of that class with the parameters specified by you. Going back to my answer for #1, the theoretical constructor for a PC would allow you to specify information such as how much RAM, ROM, etc you want.

A class may have multiple constructors and this one happens to accept both a length, and a character and produce a string consisting of that character n-many times.

For example,

cpp Syntax (Toggle Plain Text)
  1. const std::string five_stars( 5, '*' );

As for number 3, I'll let others answer. I hope I helped at least somewhat.

EDIT: Are you reading Accelerated C++ ? Just curious...
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS!

Last edited by Jessehk; Oct 9th, 2006 at 5:57 PM.
Jessehk is offline   Reply With Quote
Old Oct 9th, 2006, 6:25 PM   #4
funkey_monkey
Programmer
 
Join Date: May 2006
Posts: 34
Rep Power: 0 funkey_monkey is on a distinguished road
Quote:
Originally Posted by Jessehk View Post
EDIT: Are you reading Accelerated C++ ? Just curious...
Yes - that is the book I was recommended. Was it a wise purchase or will i get confused quickly?
funkey_monkey is offline   Reply With Quote
Old Oct 9th, 2006, 6:33 PM   #5
funkey_monkey
Programmer
 
Join Date: May 2006
Posts: 34
Rep Power: 0 funkey_monkey is on a distinguished road
Quote:
Originally Posted by Jessehk View Post

2) The const keyword in front of the string instance tells the compiler (and other bits of the program ) that after it has been given an initial value, the object cannot be modified.

Inside the brackets, the string constructor is called. A constructor for a class creates new objects of that class with the parameters specified by you. Going back to my answer for #1, the theoretical constructor for a PC would allow you to specify information such as how much RAM, ROM, etc you want.

A class may have multiple constructors and this one happens to accept both a length, and a character and produce a string consisting of that character n-many times.

For example,

cpp Syntax (Toggle Plain Text)
  1. const std::string five_stars( 5, '*' );
The thing that confuses me about
const std::string five_stars( 5, '*' );
is that to me it is calling a method with 2 passed parameters - one being the length of the string greeting and the second being a blank space character.

I guess its something I've got from Java and Ada. It still confuses me.

I take it that this is just another way of initialising a string and to just accept it?
funkey_monkey is offline   Reply With Quote
Old Oct 9th, 2006, 6:40 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I didn't see the third question (old and blind). Certainly there is a more efficient way, in terms of computer time (I'm not sure why you're asking about compiler time; it doesn't affect run-time performance, except via time spent in optimization). The purpose of the abstraction is to make the solution efficient in terms of YOUR time.

As for objects, it is my view that the subject is relatively simple, however complex your language of choice and a whole slew of wannabe high-priests might make it. Those priests, after all, keep their paychecks rolling in by convincing you to pay them for interpreting things that you, a mere mortal, can't be expected to understand.

For more of my rambling blather on the subject, see the link in my signature, "Why OOP? My View".

As for your latest question, the string class didn't provide a way to just stash a character ('a', vs. "a") into a string. I consider it an oversight. They require a length specification, even if it's just 1. I commend you on your desire to ask questions rather than just accept things. The key is to understand it. If you understand that it's stupid, or neat, or weird, but still a fact, quit worrying about it in a non-productive manner.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem reading unknown # of inputs TCStyle C++ 13 Mar 14th, 2006 9:12 AM
Error handling inputs Konnor Java 4 Mar 11th, 2006 11:43 PM
struct program skipping inputs codylee270 C 7 Dec 5th, 2005 8:02 AM
A standards question, optional inputs into Methods Arla C# 4 Apr 27th, 2005 11:38 PM
difference between inputs for java!! java_roshan Java 1 Mar 15th, 2005 4:23 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:01 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC