![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: May 2006
Posts: 34
Rep Power: 0
![]() |
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(), ' '); 3. Is there not a quicker/simpler/more efficient (in terms of compiler time and memory usage) implementation to do this task? Tthanks, FM. |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
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)
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. |
|
|
|
|
|
#4 |
|
Programmer
Join Date: May 2006
Posts: 34
Rep Power: 0
![]() |
|
|
|
|
|
|
#5 | |
|
Programmer
Join Date: May 2006
Posts: 34
Rep Power: 0
![]() |
Quote:
const std::string five_stars( 5, '*' ); 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? |
|
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |