C++ classes question

Discussion in 'OT Technology' started by A Cow, Oct 10, 2006.

  1. A Cow

    A Cow OT Supporter

    Joined:
    Jul 7, 2005
    Messages:
    33,035
    Likes Received:
    97
    Location:
    Vancouver BC
    ok.. i made a class (called link) it is perty much a link list..

    for example it has a funtion called "insertatback" how do i call that?

    i tired

    link::insertatback()

    but that doenst work so good.. any other dieas?
     
  2. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    why are you writing linked lists and you dont know c++ or oop at all? you have to make an object of type linkedlist and then call the function on that object:

    LinkedList.insertatback(20);

    if your linked list was storing integers. It'd be a better idea to use the STL's linked list.
     
  3. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
  4. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    You are confusing your definition of the function with calling the function at runtime.

    If you want your class to only have prototypes for the functions and you want to actually define those functions outside your class then you use the classname::functionname() syntax. However, I believe the actual syntax is:

    returntype classname::functionname(parameter 1, parameter 2, ...){
    //body of your function.
    }

    The :: is called the scope resolution operator in case you want to google it for more info.

    However, if you function is already defined and you simply want to call it from your program, you need to make sure you either:
    A) create an instance of your class (also known as an object) or
    B) make your function static

    I'll let you google static functions and I'll just deal with A. So create an object first by the following:

    List a = new List(constructor parameters);

    Then you can call instance functions of that object (assuming those functions are declared as public) by using the following:

    a.insertatback(arguments);

    Like I said, you need to make sure and put public before the function prototype in your class definition or else it might be private and as you prolly know, you can't call private members from outside your class.
     
  5. A Cow

    A Cow OT Supporter

    Joined:
    Jul 7, 2005
    Messages:
    33,035
    Likes Received:
    97
    Location:
    Vancouver BC
    kk obously i didnt get my point right :p


    im currently in school for comptuer game programing (lol :/) my teacher well.. he doesnt speak english that well.. so uh.. yea.. :p he also doesnt realy do much teaching.. hes using slides that are 5+ years old..

    here is the code i have so far..

    classlink.h
    Code:
    
    struct datastructure
    {
    	char fname[20];
    	char lname[20];
    	char corse[20];
    	int price;
    	int mark;
    };
    
    struct node
    {
    	datastructure data;
    	struct node* previous;
    	struct node* next;
    };
    
    
    
    
    class link
    {
    public:
    	link();
    	void insertatfront();
    	void insertatback();
    	void printf();
    	void clearall();
    	void printb();
    	void deleafter();
    	void delebefore();
    	void addafter();
    	void deleb();
    	void addbefore();
    
    private:
    	node* start;
    };
    classlink.cpp

    Code:
    #include "stdafx.h"
    #include "classlink.h"
    #using <mscorlib.dll>
    
    using namespace std;
    using namespace System;
    
    
    link::link()
    {
    	start = NULL;
    }
    //=======================================================================
    //=======================================================================
    void link::insertatfront()
    {
    
        node *newnode;
    	if (start == NULL)
    	{
    		//if list is empty
    		newnode = (struct node*) malloc(sizeof(struct node));
    		start = newnode;
    		newnode -> next = NULL;
    		newnode -> previous = NULL;
    
    		Console::WriteLine("Enter your First name");
    		cin>>newnode->data.fname;
    
    		Console::WriteLine("Enter your Last name");
    		cin>>newnode->data.lname;
    
    		Console::WriteLine("Enter your corse");
    		cin>>newnode->data.corse;
    
    		Console::WriteLine("Enter your mark");
    		cin>>newnode->data.mark;
    
    		Console::WriteLine("Enter the cost");
    		newnode->data.price = Convert::ToInt32(Console::ReadLine());
    
    		//newnode -> data = Convert::ToInt32(Console::ReadLine());
    
    /*
    	string fname;
    	string lname;
    	string corse;
    	int price;
    	int mark;
    */
    	}else
    	{
    		//if list exists
    		newnode = (struct node*) malloc(sizeof(struct node));
    		//creates a new node
    		Console::WriteLine("Enter your First name");
    		cin>>newnode->data.fname;
    
    		Console::WriteLine("Enter your Last name");
    		cin>>newnode->data.lname;
    
    		Console::WriteLine("Enter your corse");
    		cin>>newnode->data.corse;
    
    		Console::WriteLine("Enter your mark");
    		cin>>newnode->data.mark;
    
    		Console::WriteLine("Enter the cost");
    		newnode->data.price = Convert::ToInt32(Console::ReadLine());
    
    		newnode ->next = start;
    		newnode -> previous = NULL;
    		start -> previous = newnode;
    		start = newnode;
    	}
    
    }
    //=======================================================================
    //=======================================================================
    void link::insertatback()
    {
    	node* current = start;
        node* newnode;
    	
    	if (start == NULL)
    	{
    		//if list is empty
    		newnode = (struct node*) malloc(sizeof(struct node));
    		start = newnode;
    		newnode -> next = NULL;
    		newnode -> previous = NULL;
    		
    		Console::WriteLine("Enter your First name");
    		cin>>newnode->data.fname;
    
    		Console::WriteLine("Enter your Last name");
    		cin>>newnode->data.lname;
    
    		Console::WriteLine("Enter your corse");
    		cin>>newnode->data.corse;
    
    		Console::WriteLine("Enter your mark");
    		cin>>newnode->data.mark;
    
    		Console::WriteLine("Enter the cost");
    		newnode->data.price = Convert::ToInt32(Console::ReadLine());
    	}else
    	{
    		
    		while (current->next != NULL)
    		{
    			current = current -> next;
    		}
    
    		node* newnode;
    		newnode = (struct node*) malloc(sizeof(struct node));
    		current -> next = newnode;
    		newnode -> next = NULL;
    		newnode -> previous = current;
    		Console::WriteLine("Enter your First name");
    		cin>>newnode->data.fname;
    
    		Console::WriteLine("Enter your Last name");
    		cin>>newnode->data.lname;
    
    		Console::WriteLine("Enter your corse");
    		cin>>newnode->data.corse;
    
    		Console::WriteLine("Enter your mark");
    		cin>>newnode->data.mark;
    
    		Console::WriteLine("Enter the cost");
    		newnode->data.price = Convert::ToInt32(Console::ReadLine());
    	}
    }
    //=======================================================================
    //=======================================================================
    void link::printf()
    {
    	node* current;
    	current = start;
    	while(current != NULL)
    	{
    		
    
    
    
    		Console::WriteLine(current->data.price);
    		current = current ->next;
    
    	}
    	if(start = NULL)
    	{
    		Console::WriteLine("List is empty G");
    	}
    }
    //=======================================================================
    //=======================================================================
    void link::deleb()
    {
    	node* current = start;
    	
    	while(start != NULL)
    	{
    		current ->previous ->next = NULL;
    	}
    }
    
    //=======================================================================
    //=======================================================================
    
    void link::printb()
    {
    	node* current;
    	current = start;
    	while(current->next  != NULL)
    	{
    		current = current->next;
    	}
    	while(current->previous  != NULL)
    	{
    
    
    
    
    	}
    		for(int i = 0; i < 20; i++)
    		{
    			Console::Write(current->data.fname[i]);
    		}
    		
    		Console::WriteLine(" ");
    		
    		for (int i = 0; i < 20; i++)
    		{
    			Console::Write(current->data.lname[i]);
    		}
    		
    		Console::WriteLine(" ");
    
    		for(int i = 0; i < 20; i++)
    		{
    			Console::Write(current->data.corse);
    		}
    	Console::WriteLine(current->data.price);
    }
    
    //=======================================================================
    //=======================================================================
    
    void link::clearall()
    {
    	//start = NULL;
    	node *current,*remove;
    	current = start;
    	if(start != NULL)
    	while(current->next != NULL)
    	{
    		remove=current;
    		current = current->next;
            free(remove);
    	}
    	start = NULL;
    }
    //=======================================================================
    //=======================================================================
    
    void link::deleafter()
    {
    	node* current;
    	Console::WriteLine("waht node?");
    	char tofind[20];
    	cin>>tofind;
    	current = start;
    	while(current->data.fname != tofind){
    		if(current->next = NULL){
    			Console::WriteLine("not found");
    			return;
    		}
    		current = current->next;
    	}
    	if(current->next->next = NULL){
    		current->next = NULL;
    		free(current->next);
    		return;
    	}
    	
    	current->next->next->previous = current;
    	current->next = current->next->next;
    	free(current->next);
    
    }
    
    
    //=======================================================================
    //=======================================================================
    void link::delebefore()
    {
    	node* current;
    	current = start;
    	Console::WriteLine("what node?");
    	int tofind;
    	tofind = Convert::ToInt32(Console::ReadLine());
    	while(current->data.price != NULL){
    		if(current->next = NULL){
    			Console::WriteLine("not found");
    			return;
    		}
    		current = current->next;
    	}
    
    	current->previous->previous->next = current;
    	current->previous = current->previous->previous;
    
    
    
    }
    //=======================================================================
    //=======================================================================
    void link::addafter()
    {
    	node* current;
    	Console::WriteLine("waht node?");
    	char tofind[20];
    	cin>>tofind;
    	current = start;
    	while(current->data.fname != NULL){
    		if(current->next>NULL){
    			Console::WriteLine("not found");
    			return;
    		}
    		current = current->next;
    	}
    	node* newnode;
    	newnode=(node*) malloc(sizeof(node));
    	newnode->next = current->next;
    	newnode->previous=current;
    	current->next->previous = newnode;
    	current->next = newnode;
    
    
    }
    //=======================================================================
    //=======================================================================
    
    void link::addbefore()
    {
    	node* current;
    	current = start;
    	Console::WriteLine("what node?");
    	char tofind[20];
    	cin>>tofind;
    	while(current->data.fname != NULL){
    		if(current->next = NULL){
    			Console::WriteLine("not found");
    			return;
    		}
    		current = current->next;
    	}
    	node* newnode;
    	newnode=(node*) malloc(sizeof(node));
    	newnode->next = current;
    	newnode->previous=current->previous;
    	current->previous->next = newnode;
    	current->previous = newnode;
    	
    }
    //=======================================================================
    //=======================================================================

    linkedlist.cpp
    Code:
    // linkedlsit.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <string>
    #include <iostream>
    #include "classlink.h"
    #using <mscorlib.dll>
    
    using namespace std;
    using namespace System;
    
    //=======================================================================
    //                         Structs
    //=======================================================================
    
    
    
    
    //=======================================================================
    //                          Prototypes
    //=======================================================================
    
    //=======================================================================
    //                          Global Vars
    //=======================================================================
    
    //=======================================================================
    //=======================================================================
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int op;
    	op=0;
    	while(op != 10)
    	{	
    	op = 0;
    	Console::WriteLine("1)  add to back");
    	Console::WriteLine("2)  Dele back");
    	Console::WriteLine("3)  Add before specific");
    	Console::WriteLine("4)  Add after specific");
    	Console::WriteLine("5)  Dele before a specific");
    	Console::WriteLine("6)  dele after a specific");
    	Console::WriteLine("7)  print foward");
    	Console::WriteLine("8)  brint backward");
    	Console::WriteLine("9)  purge all items");
    	Console::WriteLine("10) KILL TEH PROGRAM ZORS");
    	op = Convert::ToInt32(Console::ReadLine());
    
    
    		if (op==1){
    			insertatback();
    		}	
    
    		if (op == 2){
    			link::deleb();
    		}
    
    		if (op == 3){
    			link::addbefore();
    		}
    
    		if (op == 4){
    			link::addafter();
    		}
    
    		if (op == 5){
    			link::delebefore();
    		}
    
    		if (op == 6){
    			link::deleafter();
    		}
    
    		if (op == 7){
    			link::printf();
    		}
    
    		if (op == 8){
    			link::printb();
    		}
    
    		if (op == 9){
    			link::clearall();
    		}
    		
    	}
    	
    	//insertatfront();
    	
    
    	//======================== shit i gota do =========-=====================
    	//	1.	DONE You should be able to add anode at the back of the list
    	//	2.	DONE You should be able to delete a node from the back of the list
    	//	3.	You should be able to add a node before a specific node
    	//	4.	You should be able to add a node after specific node
    	//	5.	You should be able to delete a node before a specific node
    	//	6.	You should be able to delete a node after specific node
    	//	7.	DONE You should be able to print the list
    	//	8.	You should be able to print the list backward
    	//	9.	You should be able to purge (delete) all items from the list
    	//=======================================================================
    
    	return 0;
    }
    
    //=======================================================================
    //=======================================================================
    
     
  6. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    You've defined a class, but created no instance of it.
    In order to perform actions on a list object, you need to create one first.

    so something like...

    link *mylist = new link();

    will create a new instance of your link class which you can access through the mylist pointer.

    Now you can act on your list by calling its member functions:

    mylist->insertatfront();
    mylist->insertatback();
    ...etc.
     
  7. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    Look here moo cow...if you had bothered to read my post you would need to post all that shit here.
     
  8. A Cow

    A Cow OT Supporter

    Joined:
    Jul 7, 2005
    Messages:
    33,035
    Likes Received:
    97
    Location:
    Vancouver BC
    i did :/ but i dont get it.. :/

    wait.. would it be

    i also dont have a link() function..
     
  9. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    what don't you get about my post? Be specific.
     
  10. Bruticus

    Bruticus half dead OT Supporter

    Joined:
    Apr 10, 2004
    Messages:
    4,608
    Likes Received:
    0
    Location:
    Melbourne
    For some reason that killed me :rofl: :rofl: I think I need to sleep :)
     

Share This Page