1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Couple of Questions

Discussion in 'C++' started by NameGoesHere, Sep 12, 2008.

  1. NameGoesHere

    NameGoesHere New Member

    Joined:
    Sep 9, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hey guys, I was looking on the internet and my book, but I dont understand what the following are or are used for :

    1.Pointers
    2.Overloading Functions
    3.Inheritance

    If you guys could explain these it would be cool.

    Thanks.
     
  2. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    A pointer points to something in memory. If you haven't done assembly language programming that probably won't make a lot of sense at this stage but as you get to know C you will find out that it is one of C's most powerful features. If you define int x; then you can define a pointer to it with something like int *ptr_to_x = &x; ptr_to_x is of type "pointer to int", and & takes the address of something. To access the memory, you just "dereference" the pointer, so if you do *ptr_to_x=3; that is equivalent to setting x to 3. How is that useful? Well if you don't have x in scope but you have a pointer to it, then you can modify it even though it's not in your scope, which means you can write a function to transform a data structure, then pass that function a pointer to that data structure, which saves copying the structure over and over, which is what languages without pointers have to do.

    Overloaded functions mean you can do different things which are semantically equivalent by using the same function name, which makes code easier to write. For example if you consider addition; you might have two forms to code, add an integer and add a float. Without function overloading you'd have to code the functions with separate names, e.g. x.add_int(int q) and x.add_float(float q), but with function overloading you can define both as just x.add(int q) and x.add(float q) and the different functions are distinguished by the argument list. (This is where name mangling comes from, because linkers generally can't handle this, so the function name needs mangling so that the linker doesn't get confused. x.add(int) might be mangled into x.add$I, and x.add(float) to x.add$F, so the compiler knows these are both x.add() and the linker doesn't get them confused with each other.

    Inheritance means you can define a class, then define a new class which is an extension of that other class, rather than rewriting the whole first class for each new class. Standard example is class Vehicle{} which might define some standard operations for vehicles. class Car : public Vehicle{} means Car is a kind of Vehicle and Car gets all Vehicle functions and it gets to define its own. class Motorbike : public Vehicle{} would define different functions but also get Vehicle's functions. This is useful for polymorphism; you define an array of Vehicles, into which you can plonk Cars, Motorcycles and any other different but related objects, then iterate over the whole lot without any of the fancy function pointer shenanigans you needed when doing this in plain C.
     
  3. NameGoesHere

    NameGoesHere New Member

    Joined:
    Sep 9, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    xpi0t0s I thank you, for explaning this to me. I understand them now. :D
     
  4. NameGoesHere

    NameGoesHere New Member

    Joined:
    Sep 9, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hey I got another question, I get an error when I put a string into a header and then try to use it in a cpp with that header.
    Heres my code :
    Person.h
    Code:
    #include <string>
    
    class Person{
    private :
    	string name;
    public  :
    	Person(void);
    	~Person(void);
    };
    
    Person.cpp
    Code:
    #include <iostream>
    #include <string>
    #include "Person.h"
    
    using namespace std;
    
    Person::Person(){
    	cout << "Enter your name : ";
    	cin >> name;
    }
    
    Person::~Person(){
    }
    
    I get errors like :
    error C2146: syntax error : missing ';' before identifier 'name'
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    error C2065: 'name' : undeclared identifier
     
  5. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    Try putting using namespace std; before #include "Person.h".
     
  6. hkp819

    hkp819 New Member

    Joined:
    Dec 4, 2008
    Messages:
    59
    Likes Received:
    1
    Trophy Points:
    0
    if header file is #include<iostream> than you should write using namespace std;

    If header file is #include<iostream.h> than not need of using namespace std;
     

Share This Page